gpt4 book ai didi

swift - 将 Swift 字典转换为 NSDictionary

转载 作者:IT王子 更新时间:2023-10-29 05:50:06 27 4
gpt4 key购买 nike

我创建了一个 Swift Dictionary 对象

var params = ["first_name": firstNameTextField.text,
"last_name": lastNameTextField.text,
"company": companyTextField.text,
"email_address": emailTextField.text,
"phone_number": phoneTextField.text]

接下来,我将 ObjC 框架连接到我的 Swift 应用程序。里面有个对象有属性

@property (nonatomic, retain) NSMutableDictionary *fields;

我正在尝试以这种方式分配它

object.fields = params

出现错误:

Cannot convert the expression's type '()' to type 'NSMutableDictionary!'

我也试过这样做

var params = ["first_name": firstNameTextField.text,
"last_name": lastNameTextField.text,
"company": companyTextField.text,
"email_address": emailTextField.text,
"phone_number": pnoneTextField.text].mutableCopy() as NSMutableDictionary

object.fields = params

它编译的很好,但是我得到了运行时错误:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', 
reason: '*** -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]:
attempt to insert nil object from objects[0]'

我打印了所有字段,但它们不是零:

println(firstNameTextField.text)
println(lastNameTextField.text)
println(companyTextField.text)
println(emailTextField.text)
println(pnoneTextField.text)

...

1
2
3
5
4

有什么想法吗?

最佳答案

试试 parms.bridgeToObjectiveC()。不过,这可能会给您一个 NSDictionary 而不是 NSMutableDictionary。

关于swift - 将 Swift 字典转换为 NSDictionary,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24171639/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com