When you index a dictionary, as in jsonResult["subjects"] , you get an optional one. You need to expand the option. Moreover, since this dictionary comes from JSON, Swift does not know what it contains: it is typed as AnyObject, so does Swift describe optional as AnyObject? . That way, you also tell Swift which type of object it really is β itβs a set of dictionaries, and you need to tell Swift, or you wonβt be able to index it with [0] .
You can do both of these things in one move, for example:
if let array = jsonResult["subjects"] as? [[NSObject:AnyObject]] { let result = array[0]
If you are very, very confident in your land, you can force the casting to be expanded and reduced to one line, for example:
let result = (jsonResult["subjects"] as! [[NSObject:AnyObject]])[0]
But I can not recommend it. Too many ways to do it wrong.
source share