33

I am getting this error when creating a dictionary in Swift:

Value of type 'String' does not conform to expected dictionary value type 'AnyObject'

Code:

let joeSmith : [String : AnyObject] = ["Name" : "Joe Smith", "Height" : 42, "Soccer Expo" : true, "Guardian" : "Jim and Jan Smith"]
shim
  • 9,289
  • 12
  • 69
  • 108
Jack Colosky
  • 381
  • 1
  • 3
  • 5
  • 2
    Hey Jack, @appzYourLife provided an really good answer to your question but you didn't accept it yet. If that's just an oversight, you really should accept it. – Jim Jan 28 '17 at 03:10

3 Answers3

64

Swift 3

First of all a String in Swift is a struct and does not conform to AnyObject.

Solution #1

The best solution in Swift 3 is changing the type of the Dictionary Value from AnyObject to Any (which includes the String struct).

let joeSmith : [String : Any] = ["Name" : "Joe Smith", "Height" : 42, "Soccer Expo" : true, "Guardian" : "Jim and Jan Smith"]

Solution #2

However if you really want to keep the value fo the Dictionary defined as AnyObject you can force a bridge from the String struct to the NSString class adding as AnyObject as shown below (I did the same for the other values)

let joeSmith : [String : AnyObject] = [
    "Name" : "Joe Smith" as AnyObject,
    "Height" : 42 as AnyObject,
    "Soccer Expo" : true as AnyObject,
    "Guardian" : "Jim and Jan Smith" as AnyObject]

Swift 2

The problem here is that you defined the value of your dictionary to be AnyObject and String in Swift is NOT an object, it's a struct.

enter image description here

The compiler is complaining about String because is the first error but if your remove it, it will give you an error for the 42 which again is an Int an then a Struct.

enter image description here

And you will have the same problem with true (Bool -> Struct).

enter image description here

You can solve this problem in 2 ways:

Foundation #1

If you add import Foundation then the Swift struct is automatically bridged to NSString (which is an object) and the compiler is happy

enter image description here

Any #2

You replace AnyObject with Any. Now you can put any kind of value in your dictionary.

enter image description here

Considerations

IMHO we (Swift developers) should progressively stop relying on Objective-C bridging and use the second solution.

Luca Angeletti
  • 58,465
  • 13
  • 121
  • 148
  • 1
    I'm using the second solution but if you have huge app with a lot of models and you use Any then the compiler keeps compiling almost endless in swift 2.3 but in swift 3 it doesn't compile anymore. The compiler seems not to optimize the Any. – user1007522 Sep 28 '16 at 09:06
  • 4
    @appzYourLife Has this behavior changed, or am I just missing something? I am getting an error when trying to do something like this, even when Foundation has been imported: `let dict: [String : AnyObject] = ["hello" : "world"]`. The error is `Value of type 'String' does not conform to expected dictionary value type 'AnyObject'` – JAL Oct 07 '16 at 18:37
  • 1
    I just saw all of the upvotes on my comment. Yes this has changed. In Swift 3, Swift types are no longer implicitly bridged to Foundation types with the import. See [Why aren't Swift Strings bridged to NSString in a Dictionary when Foundation is imported?](http://stackoverflow.com/q/39924105/2415822) and the linked duplicate. – JAL Oct 19 '16 at 05:07
1

used it will helpfull

let plistDict: [String: Any] = ["key": "Value"]
Narendra Jagne
  • 436
  • 3
  • 8
0

OMHO If you are using Any or AnyObject you are missint the point of using a strongly typed language

because enums in swift can hold associated data a good answer here is to change the way you are using the language and create an enum that captures the different cases then use the enum as a type. If the dictionary is actualy representing a class of its own than maybe you need to define the class. either way what yo are doing should scream to you that something is wrong with your code.

https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Enumerations.html