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
Pure Swiftui Login, Signup, Register Flow, Is It Possible
Swift - Update/Refresh Label That Displays Time
How to Reuse Struct on Swift? or Is There Any Other Way
Avaudioplayer Working on Simulator But Not on Real Device
Programmatically Auto-Connect to Wifi iOS
Swift 2 - Separating an Array into a Dictionary with Keys from a to Z
How to Change a View from Portrait Mode to Landscape Mode and Lock It
How to Send Request from iOS (Swift) to Dialogflow V2 API Without Cloud Functions
Nsexceptionallowsinsecurehttploads Not Working for Ip Addresses
Operation Went Isfinished=Yes Without Being Started by the Queue It Is In
Uitextview - Adjust Size Based on the Content in Swiftui
How to Get the Opposite Value of a Bool in Swift
Convert Nsdate to String with a Specific Timezone in Swift
How to Convert Base64 into Nsdata in Swift
Using Avaudioplayer to Play Remote Mp3 File in Swift
How to Indicate a View Which Has Created with Uikit from a Skscene