user2456014 user2456014 - 1 month ago 8
Swift Question

How to assign a Dictionary to AnyObject in swift

I have been playing around with Swift. I have had multiple errors with types, especially working with Swift and my old Objective-C classes. The problem with this method is: I am expecting an array made of NSDictionarys in Objective-C.

var curArr:[Dictionary<String, AnyObject>] = self.getItemsForCurrentStack()
var arrToReturn:[(Dictionary<String, AnyObject?>)] = []
for obj in curArr{
arrToReturn.append(["image": UIImage(named: obj["imageName"] as! String), "color": UIColor(red:obj["colorDic"]!["red"] as! CGFloat, green:obj["colorDic"]!["green"] as! CGFloat, blue:obj["colorDic"]!["blue"] as! CGFloat, alpha:1.0), "percentage": obj["percantage"]])
}
return arrToReturn


This returns Dictionaries (which are NSDictionaries) in Swift. But the last line throws me an error:


Dictionary' is not identical to 'AnyObject'


I've tried using
as! [AnyObject]


But that throws another error:


'AnyObject' is not a subtype of 'Dictionary'


I don't get the second error, since this doesn't have to be a subtype, but the other way around. Any ideas on how to solve this? I didn't find an answer for several hours of googleing and researching.

Answer

Dictionary is a struct in Swift, whereas AnyObject is

/// The protocol to which all classes implicitly conform.

Depending what you're trying to do, you may want to use Any in your code, or cast your dictionary to NSDictionary using as NSDictionary.


Edit after your clarification:

If you split up the append call from the dictionary itself, you see a better error message:

So, the issue is that your dictionary contains some Optional values, but Optional is a struct and not convertible to Obj-C. You can fix by casting to UIImage! and AnyObject! (ImplicitlyUnwrappedOptional), or by using as!.