Search code examples
iosswiftnsmutablearraynsarrayswift3

After switching to Swift3 I have: Type NSFastEnumerationIterator.Element (aka Any) has no subscript members


this is actually a follow up to this question: how can I convert NSArray into specific String? Bryan's answer worked for me in previous version of Swift, but now when I moved to Swift 3, this code:

if (defaults.object(forKey: "fb_friends") != nil) {
    var friends = (defaults.objectForKey("fb_friends") as! NSMutableArray)
                    .map{$0["id"]!!}
                    .joinWithSeparator(",")
                friends+=",\(defaults.stringForKey("facebookId")!)"
                params["friends"] = friends 
            }

and especially this line .map{$0["id"]!!} throws me an error:

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

what might be the problem here?


Solution

  • The error message is clear that you can no longer to access subscript members from Any in Swift 3.

    In this case, you can cast it to NSDictionary.

    var friends = (defaults.objectForKey("fb_friends") as! NSArray)
        .map{($0 as! NSDictionary)["id"] as! String}
        .joined(separator: ",")