Recently i update Xcode to 7 and it contains swift 2.0 compiler. Before this i made my project with older version of swift. In that version i had create NSMutableDictionary
like bellow
let dictParams:NSMutableDictionary? = ["test" : "test",
"username" : txtEmail.text,
"password" : txtPassword.text,
"version" : "1.0",
"appId" : "1",
"deviceId" : "fasdfasdfrqwe2345sdgdfe56gsdfgsdfg"
];
in above code txtEmail.text
and txtPassword.text
is my text field and fill tha value at run time.
This code is properly working in older version of swift but after update to swift 2.0 it gives me an error like bellow
Cannot convert value of type '[String : String?]' to specified type 'NSMutableDictionary?'
what's wrong with it please guide me.
The NSMutableDictionary class declares the programmatic interface to objects that manage mutable associations of keys and values. It adds modification operations to the basic operations it inherits from NSDictionary . NSMutableDictionary is “toll-free bridged” with its Core Foundation counterpart, CFMutableDictionary .
Use -mutableCopy . NSDictionary *d; NSMutableDictionary *m = [d mutableCopy]; Note that -mutableCopy returns id ( Any in Swift) so you will want to assign / cast to the right type. It creates a shallow copy of the original dictionary.
Use NSMutableDictionary addEntriesFromDictionary to add the two dictionaries to a new mutable dictionary. You can then create an NSDictionary from the mutable one, but it's not usually necessary to have a dictionary non-mutable. Save this answer.
If you assign a created dictionary to a variable, then it is always mutable which means you can change it by adding, removing, or changing its items. But if you assign a dictionary to a constant, then that dictionary is immutable, and its size and contents cannot be changed.
Simply by opening NSMutableDictionary
or NSDictionary
class interfaces from Xcode 7, you could easily see that the underlying type is actually [NSObject: AnyObject]
. It means that the value can't be nil
.
Unwrapping text values like txtEmail.text!
or txtPassword.text!
might look ok and help you to get rid of the compiling error, but it's a bad choice because technically text
property of UITextField
is optional and your app can crash in that case!
Do this for your safety:
let dictParams: NSMutableDictionary? = ["test" : "test",
"username" : txtEmail.text ?? "", // Always use optional values carefully!
"password" : txtPassword.text ?? "",
"version" : "1.0",
"appId" : "1",
"deviceId" : "fasdfasdfrqwe2345sdgdfe56gsdfgsdfg"
]
By the way, in case it's not critical to use NSMutableDictionary
, please consider using Swift dictionary like this:
var mutableDictionary = [String: AnyObject]
// OR this if the value can be nil
var mutableDictionary = [String: AnyObject?]
simple change in
"username" : txtEmail.text!
instead "username" : txtEmail.text
so final code is like bellow
let dictParams:NSMutableDictionary? = ["test" : "test",
"username" : txtEmail.text!, //Add ! here
"password" : txtPassword.text!, //Add ! here
"version" : "1.0",
"appId" : "1",
"deviceId" : "fasdfasdfrqwe2345sdgdfe56gsdfgsdfg"
];
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