Byron Coetsee
Byron Coetsee

Reputation: 3593

iOS 10 NSMutableDictionary and NSMutableArray not working as before

It seems with the release of iOS 10, a few things have broken. The major one for me has been the use of NSMutableDictionary and NSMutableArray. Both no longer seem to be able to parse a string of JSON and instead give out a nil while in pre iOS 10 they populated as expected. The only way around this I've found is to use NSDictionary and NSArray respectively and then use the init methods to cast back. For example:

let json = "{ \"code\": \"abcde\", \"name\": \"JP Morgan\" }"
json as! NSMutableDictionary // gives nil
NSMutableDictionary(dictionary: json as! NSDictionary) // works :)

let json = "[{ \"code\": \"abcde\", \"name\": \"JP Morgan\" }]"
json as! NSMutableArray // gives nil
NSMutableArray(array: json as! NSArray) // works :)

I would like to know why?

And I hope this helps someone solve their issue...

Upvotes: 0

Views: 2043

Answers (1)

vadian
vadian

Reputation: 285072

The Foundation types NSMutableArray / NSMutableDictionary are not related to the Swift counterparts and cannot be bridged / coerced from a literally created Swift type. But that's not new in Swift 3.

Basically do not use NSMutableArray / NSMutableDictionary in Swift unless you have absolutely no choice for example interacting with a few low level CoreFoundation API. The native Array / Dictionary types used with var provide the same functionality (except value vs. reference semantics) and in addition the types of the containing objects.

Upvotes: 2

Related Questions