-2

I have a JSON object/dictionary I retrieved from AFNetworking and I want to conditionally unwrap the key into an array of strings.

var person: [String : AnyObject] = ...

if let interests = person["interests"] as [String]{
   // Do something
}

I get the following error message: (String : AnyObject) is not convertible to [String]. I don't think I can typecast person to [String : Any] because it comes out as [String : AnyObject] from the AFNetworking framework. Any ideas would be appreciated.

blee908
  • 12,165
  • 10
  • 34
  • 41
  • You are not telling the truth. No one can reproduce based on what you've shown. Show your _real_ code. Do not type by hand: copy and paste so that we see _exactly_ what you are saying. – matt Dec 15 '14 at 18:15
  • @matt. You can easily reproduce the code. I've written basically exactly what i posted in `Playground` and took a screenshot. http://i.imgur.com/2aSAjRe.png – blee908 Dec 15 '14 at 18:18
  • Because you didn't import UIKit. See my screenshot which trumps your screenshot! – matt Dec 15 '14 at 18:21
  • @matt I want to solve this issue just as much as anyone else, I'm still getting a compile error: http://i.imgur.com/gu9wtHe.png – blee908 Dec 15 '14 at 18:26

1 Answers1

3

Update your Xcode. You are using an old version. Prior to Xcode 6.1, String was not considered an object type: you had to use NSString instead:

if let interests = person["interests"] as? [NSString] {

Apple fixed that issue, so this now works with Xcode 6.1:

if let interests = person["interests"] as? [String] {

Since Swift is rapidly evolving, you are advised to keep up to date with the latest released verison of Xcode, which as of this writing is Xcode 6.1.1.

vacawama
  • 150,663
  • 30
  • 266
  • 294