I have created a Swift Dictionary object
var params = ["first_name": firstNameTextField.text,
"last_name": lastNameTextField.text,
"company": companyTextField.text,
"email_address": emailTextField.text,
"phone_number": phoneTextField.text]
Next, I have ObjC framework which I connected to my Swift app. There is an object inside with property
@property (nonatomic, retain) NSMutableDictionary *fields;
I'm trying to assign it this way
object.fields = params
And got an error:
Cannot convert the expression's type '()' to type 'NSMutableDictionary!'
I also tried to do it this way
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
It compiled well, but I got runtime error:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: '*** -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]:
attempt to insert nil object from objects[0]'
I have all my fields printed and they aren't nil:
println(firstNameTextField.text)
println(lastNameTextField.text)
println(companyTextField.text)
println(emailTextField.text)
println(pnoneTextField.text)
...
1
2
3
5
4
Any ideas?
Try parms.bridgeToObjectiveC(). That may give you an NSDictionary rather than an NSMutableDictionary, though.
I'm guessing that this code initializing your params...
var params = ["first_name": firstNameTextField.text,
"last_name": lastNameTextField.text,
"company": companyTextField.text,
"email_address": emailTextField.text,
"phone_number": phoneTextField.text]
...might be happening before you have any text field outlets hooked up. What I would suggest is that you set this var as @lazy, type it as Dictionary<String,String>, and set it to a called closure that returns this dictionary. Thus the dictionary won't be created until later, when you actually access self.params.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With