“Cannot assign value of type 'String' to type 'AnyObject?'”, Swift 3, Xcode 8 beta 6

后端 未结 2 1559
情话喂你
情话喂你 2020-12-09 07:59

A fairly simple piece of code

var dict: [String: AnyObject] = [:]
dict[\"key\"] = \"value\"

generates the following compile-time error

2条回答
  •  醉梦人生
    2020-12-09 08:34

    In b6, String no longer magically bridges to NSString. String is not a class; it's a struct. You need to do the bridging by hand:

    dict["key"] = "value" as AnyObject
    

    The fact that is still seems to be bridging is likely a bug and should be reported.

    It goes without saying that [String: AnyObject] and [String: Any] should be used as little as possible in your code.

    (Make sure to follow the link Hamish provides in the comments below.)

提交回复
热议问题