Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create NSMutableDictionary in swift 2.0

Tags:

swift

swift2

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.

like image 659
Bhavin_m Avatar asked Sep 24 '15 12:09

Bhavin_m


People also ask

What is NSMutableDictionary in Swift?

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 .

How do you convert NSDictionary to NSMutableDictionary?

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.

How do you append NSMutableDictionary?

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.

How do you declare a mutable dictionary in Swift?

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.


2 Answers

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?] 
like image 176
Ducky Avatar answered Oct 12 '22 13:10

Ducky


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"
    ];
like image 25
Bhavin_m Avatar answered Oct 12 '22 15:10

Bhavin_m