I'm trying to understand why I have a compilation error: Type '[String, AnyObject?]' does not conform to protocol AnyObject?
var cars: [String: AnyObject?] = [ "model": "Ferrari"]
var JSON: [String: AnyObject?] = [ "cars": cars ] // <-- error
It seems like cars
is not an AnyObject? which I don't understand.
Thanks for your help!
AnyObject
must be a class, but Dictionary
is a structure.
AnyObject can represent an instance of any class type.
Swift’s String, Array, and Dictionary types are implemented as structures.
You can use Any?
for this purpose.
var cars: [String: Any?] = [ "model": "Ferrari"]
var JSON: [String: Any?] = [ "cars": cars ]
After looking at this a bit more, there is a subtle problem which causes this to break.
Here is a sample which works:
var cars: [String: AnyObject] = [ "model": "Ferrari"]
var JSON: [String: AnyObject] = [ "cars": cars ]
It works because cars
is coerced (implicitly cast) from [String: AnyObject]
to an object compatible with NSDictionary ([AnyHashable: AnyObject])
. This NSDictionary
compatible object is then cast to AnyObject
. So, magic.
[String: AnyObject?]
cannot be cast in this way because it not NSDictionary
compatible, so the coercion fails.
It seems like cars is not an AnyObject?
You defined cars
as of type [String: AnyObject?]
, thus, the following code is valid (tested in Playground):
var cars: [String: AnyObject?] = [ "model": "Ferrari"]
var JSON: [String: [String: AnyObject?]] = [ "cars": cars ]
You should define JSON
as of type [String: AnyObject?]
or [String: Any?]
(according to your needs) only if you can't guess what type cars
will be (see Type Casting for Any and AnyObject in The Swift Programming Language).
Will that impact the casting of my object towards a NSDictionary?
NSDictionary
can't hold nil
(see here or here). The following code gives an error message in Playground:
var nsDict1: NSDictionary = [12 : 10, 10 : nil] //eror message
var cars: [String: AnyObject?] = [ "model" : nil]
var JSON: [String: [String: AnyObject?]] = [ "cars" : cars ]
let nsDict2 = JSON as NSDictionary //error message
However, the following code is valid:
var nsDict1: NSDictionary = [12 : 10, 10 : NSNull()] //no error message
var cars: [String: AnyObject] = [ "model" : "Ferrari"] //also works with [ "model" : NSNull()]
var JSON: [String: [String: AnyObject]] = [ "cars" : cars ]
let nsDict2 = JSON as NSDictionary //no error message
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