简体   繁体   English

无法使用参数“ NSString”的类型调用“下标”

[英]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. 该代码应该循环返回的数据,并将字段添加到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". 如果我删除“ as NSString”,则错误消息为:“找不到接受提供的参数的'下标'的重载”。

Please advise and answer in swift. 请迅速提出建议并回答。

The object you are trying to apply the subscript is objects , which is an array. 您尝试应用下标的objectsobjects ,它是一个数组。 You probably want to do that at the i-th element: 您可能想在第i个元素上执行此操作:

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-each循环

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM