A Different Bridging Between Array and Dictionary

What is difference between NSDictionary vs Dictionary in Swift?

Dictionary is a native Swift struct. NSDictionary is a Cocoa class. They are bridged to one another (within the usual limits), as the docs explain very clearly and fully.

It's exactly parallel to Array and NSArray.

Are Swift's String, Array, and Dictionary NSObjects? Why can a struct be an NSObject?

String, Array, Dictionary are all bridged to their Objective-C counterparts (NSString, NSArray and NSDictionary) and can seamlessly act like so.

String itself does not inherit from NSObject and is actually a struct but it is bridged from NSString which does. When you use a (Swift) String in your code it can act like an NSString thus giving you the output from the code you provided.

Ambiguous reference to member 'subscript'

You cannot cast to a typeless Swift dictionary or array. Change

as Dictionary
as Array

to

as NSDictionary
as NSArray

Or, if you know the actual types (i.e. what is this an array of? what is this a dictionary of?), cast down to those actual types.

Another approach is to use the new untyped types, [Any] and [AnyHashable:Any]. But this can be a little tricky, in my experience. Basically Apple has blown away the automatic bridging between Objective-C and Swift and replaced it with "permissive boxing", and this causes some type mismatches that you have to compensate for yourself.

How do you add a Dictionary of items into another Dictionary

You can define += operator for Dictionary, e.g.,

func += <K, V> (left: inout [K:V], right: [K:V]) { 
for (k, v) in right {
left[k] = v
}
}

Why does Swift have both NSDictionary and Dictionary classes?

Both types are there in order to use Objective-C code in swift. Swift array and dictionary are generic type and can hold non-class instances (enum, struct), while NSArray/NSDictonary can only hold NSObject.

Parse Dictionary of Array of Dictionary in Swift 3

Don't say

 resObj["data"] as! Dictionary<String,Array<Dictionary<String,Any>>>

That's too specific. Just say

 resObj["data"] as! [String:Any]

You know that when you get something by key out of that dictionary, it will be an array, but you can literally cross that bridge when you come to it.

The same rule applies to your other casts. Just cast to Swift dictionary or array using the broadest simplest possible type.

(Note that all this will be solved in Swift 4, where you can build a knowledge of the JSON structure right into your fetch.)

What is the difference between these two options for converting Swift Dictionary to NSMutableDictionary?

This works in most cases

No, this works not at all.

Unlike immutable NSDictionary Swift Dictionary and NSMutableDictionary are not related.

You can bridge cast (as without ?) [String:String] to NSDictionary but you cannot bridge cast nor conditionally downcast (as?) it to NSMutableDictionary.

The second syntax works because a new instance of NSMutableDictionary is created. The Swift dictionary parameter is implicitly bridged to NSDictionary


The same behavior is also true for Swift Array and Foundation NSMutableArray



Related Topics



Leave a reply



Submit