Type 'Any' Has no Subscript Members, Swift 3

Hi,
I try to update my Parse's App to Swift 3 but I stuck on the following error:

let likesNumber: AnyObject! = object.object(forKey: "Likes")![0]



Type "Any" has no subscript members


Can you help me ? Thanks a lot !

Your "object" method apparently returns value of type Any. Any is not an array and does not have a subscript operator. You need to cast the returned value into an array (or any other appropriate type that supports subscripts).

object(forKey:) returns an Any? in Swift 3, so you'll need to try to cast the Any? to the type you're expecting, which I guess is NSArray, and, if successful, you'll then be able to subscript the array.


For example, likesNumber in the following code is an Int? :


let likesNumber = (object.object(forKey: "Likes") as? NSArray)?.firstObject as? Int // or whatever type is appropriate


If you prefer your app to crash if parsing doesn't go as expected, then use '!'s instead of '?'s, and likesNumber will be an Int if there is no crash.

Type 'Any' Has no Subscript Members, Swift 3
 
 
Q