Robert Brax
Robert Brax

Reputation: 7318

Cannot invoke 'subscript' with an argument of type 'NSString'

Here is the code that causes problem. I commented where the error happens. This code is supposed to loop trough returned data, and append a field to an empty array of type NSSTring.

var bb = ["842278359156695", "850445345006243"]
        //Get user friends data from Parse
        var query = PFUser.query()
        query.selectKeys(["first_name", "last_name", "score", "rank"])
        query.whereKey("fbId", containedIn: bb)

        query.findObjectsInBackgroundWithBlock {
            (objects: [AnyObject]!, error: NSError!) -> Void in
            var friendsArrays: [NSString] = []

            for var i = 0; i < objects.count; ++i {
                friendsArrays.append(objects["first_name"] as NSString) // ERROR here

            }
            println(friendsArrays)

        }
    }

However, this doesn't happen, and cause the error in the title of this message, where show. If I remove "as NSString", then the error message is: "Could not find an overload for 'subscript' that accepts the supplied arguments".

Please advise and answer in swift.

Upvotes: 1

Views: 203

Answers (1)

Antonio
Antonio

Reputation: 72750

The object you are trying to apply the subscript is objects, which is an array. You probably want to do that at the i-th element:

for var i = 0; i < objects.count; ++i {
    friendsArrays.append(objects[i]["first_name"] as NSString) // ERROR here    
                                ^^^
}

However I suggest to convert that into a for-each loop

for object in objects {
    friendsArrays.append(object["first_name"] as NSString) // ERROR here    
}

Upvotes: 1

Related Questions