Swift 3 JSON Nsfastenumerationiterator Has No Subscript Members

Swift 3 JSON NSFastEnumerationIterator has no subscript members

Just use native Swift Array. Use always Swift native types unless you have absolutely no choice. NSArray lacks type information so the compiler cannot infer that the array contains dictionaries.

if let datas = dict["data"] as? [[String:Any]] {

type nsfastenumerationiterator.element aka any has no subscript members

I think your for in loop should like this. This is work for me. But be sure var tmpValues.

for candidate in (tmpValues as? [[String:Any]])! {
if ((candidate as? NSDictionary) != nil) {
let names = candidate["CandidateName"]! as? String

//self.values.append(candidate["CandidateName"])
self.values.append(name!)
print(name)

}
}

Type 'NSFastEnumerationIterator.Element' (aka 'Any') has no subscript members

levelrpfarr is most likely Any, you need to cast it to the actual type which tells the compiler the type of the items in the array

for dict in self.levelRefArr as! [[String:Any]] { ...

swift 3 Type 'Any' has no subscript members?

Typecast response as:

if let json = response.result.value as? [String:AnyObject]{..}

Type Any has no subscript members

The problem is exactly what the error message is telling you: to subscript a variable, it needs to be declared as a type which supports subscripting, but you've declared your array as containing Any. And Any, being the most basic type there is, doesn't support subscripting.

Assuming your array contains dictionaries, declare your getMedia function like this instead:

public func getMedia(completion: @escaping ([[String : Any]]) -> ())

Or, if there's a chance that the dictionary might contain non-string keys:

public func getMedia(completion: @escaping ([[AnyHashable : Any]]) -> ())

If the values in the dictionary all have the same type, replace Any with that type as appropriate.

Type 'Any' has no subscript members error on Alamofire 4.0

Try below code:

Alamofire.request(url!, method: .get, parameters: nil, encoding: JSONEncoding.default, headers: nil).responseJSON { response in

switch(response.result) {
case .success(_):
if let JSON = response.result.value as! [[String : Any]]!{
print("JSON: \(JSON)")
let dic = JSON[0] as [String:AnyObject]!
print("TitularEmail : ",dic?["TitularEmail"])
}
break

case .failure(_):
print("There is an error")
break
}
}

Error: Type [AnyHashable: Any]? has no subscript members (Swift)

You have to tell the compiler the static collection types because the compiler wants to prove the subscription ability.

if let data = resources["data"] as? [String:Any],
let me = data["me"] as? [String:Any] {
let displayName = me["displayName"] as! String
if let bitmoji = me["bitmoji"] as? [String:Any] {
let bitmojiAvatarUrl = bitmoji["avatar"] as! String
}
}


Related Topics



Leave a reply



Submit