Type 'Any' Has No Subscript Members in Swift 3 Xcode 8

Type 'Any' has no subscript members in Swift 3 Xcode 8

Try this in the parsing section in the middle:

        let parsed = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as! [String:Any]
let list = parsed["list"] as! [String:Any]?
let resources = list?["resources"] as! [AnyObject]?
let fields = resources?[0] as! [String:Any]?
let resource = fields?["resource"] as! [String:Any]?
let fields2 = resource?["fields"] as! [String:Any]?
let price = fields2?["price"] as! String?

Swift 3 Type 'Any' has no subscript members

let type = (self.data[indexPath.row] as? [String : String])?["Type"]

You need to cast self.data[indexPath.row] to a dictionary.

Type 'Any' has no subscript members after updating to Swift 3

In FIRDataSnapshot, value is of type id.

In Swift 3, id is imported as Any.

In the Firebase documentation, it says value can be any of NSDictionary, NSArray, NSNumber, or NSString -- clearly, subscripting doesn't make sense on all of these, especially in Swift. If you know it's an NSDictionary in your case, then you should cast it to that.

Type 'Any?' has no subscript members

change this

 let email = result["email"] as? String

into

  guard let resultNew = result as? [String:Any] 

let email = resultNew["email"] as! String

full answer

let parameters = ["fields": "email, first_name, last_name,  picture.type(large)"]
FBSDKGraphRequest(graphPath: "me", parameters: parameters).start { (connection, result, error) in

guard let resultNew = result as? [String:Any]

let email = resultNew["email"] as! String
}

Type any has no subscript error in swift 3

I poured over the many questions with duplicate titles and couldn't find one that clearly covered your case, so I'm going to explain what is going on.

Your variable property is clearly declared as type Any, which is a variable that can hold any type. The error message is telling you that you can't use subscripting [] with it. It is clear that you believe that it is a Dictionary with a key that is a String and a value that can be anything. In Swift 3, anything is typed Any. So your property is expected to be [String : Any]. If you cast property to [String : Any], then you will be able to use [] with it.

The first thing you should do is attempt to cast property to [String : Any] and then proceed if that works:

if let property = property as? [String : Any],
let name = property["Name"] as? String,
let latitude = property["Latitude"] as? NSNumber,
let longitude = property["Longitude"] as? NSNumber,
let image = property["Image"] as? String {

// To learn how to read data from plist file, check out our Saving Data
// in iOS Video Tutorial series.

// Code goes here
}

Type 'Any' has no subscript members Swift 3

an alternate solution to Frankies answer is this oneliner:

let message = (userInfo["aps"] as? [AnyHashable: Any])?["alert"] as? String ?? ""

in the end you have the message or an empty string if there is no message (or if the json structure is not as you expected)...



Related Topics



Leave a reply



Submit