简体   繁体   English

无法下标[AnyObject]的值? 索引类型为String

[英]Cannot subscript a value of [AnyObject]? with an index of type String

In the function below, I am trying to find the current user in the Users table on Parse.com by the username and add/update a field value called salaryMode . 在下面的函数中,我试图通过用户名在Parse.com的Users表中找到当前用户,并添加/更新一个名为salaryMode的字段值。

Below is my function and the error I am getting: 以下是我的功能和出现的错误:

Error I see : 我看到错误 在此处输入图片说明

Cannot subscript a value of [AnyObject]? 无法下标[AnyObject]的值? with an index of type String 索引类型为String

func saveUserSalaryInfo() {

    var query = PFQuery(className:"User")
    query.whereKey("username", equalTo: PFUser.currentUser()!.username!)
    query.findObjectsInBackgroundWithBlock {
        (users: [AnyObject]?, error: NSError?) -> Void in
        if error != nil {

            users["salaryMode"] = self.salaryMode

        } else {
            // Log details of the failure
            //NSLog("Error: %@ %@", error, error.userInfo)
        }
    }

}

Short answer: no, you cannot access an array using a subscript of type String 简短的回答:不,你不能访问使用String类型的标的数组

Long answer: 长答案:

users: [AnyObject]? This means that the user variable holds an optional type. 这意味着用户变量包含一个可选类型。 So it can be nil, or if anything, an Array of AnyObject (which allows for anything really). 因此它可以是nil,或者如果有的话,是AnyObject的数组(实际上允许任何东西)。 But this is an array: so index-based access needs numbers. 但这是一个数组:因此基于索引的访问需要数字。

You're confusing this for a Dictionary, which has a similar syntax to access elements using a key 您对Dictionary感到困惑,后者的语法类似于使用键访问元素的语法

// Array

let array: [String] = ["one", "two", "three"]

array[0] // "one"

array["one"]   // error

// Dictionary

let brothers = ["one": "Groucho", "two": "Chico", "three": "Harpo"]

brothers["one"] // Groucho

暂无
暂无

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

相关问题 不能下标类型为[String:AnyObject]的值?索引类型为String - Cannot subscript a value of type [String: AnyObject]? with an index of type String 无法使用索引类型为“String”的类型&#39;[String:AnyObject]&#39;下标值 - Cannot subscript a value of type '[String : AnyObject]' with an index of type 'String' 无法用索引类型“ String”下标“ [String:AnyObject]”类型的值 - Cannot subscript a value of type '[String: AnyObject]' with an index of type 'String' 无法使用索引类型为“字符串”的下标“ [[String:AnyObject]]”的值 - Cannot subscript a value of type '[[String : AnyObject]]' with an index of type 'String' 不能下标'[NSObject:AnyObject]类型的值吗?索引类型为'String' - Cannot subscript a value of type '[NSObject : AnyObject]?' with an index of type 'String' 无法用索引类型“字符串”下标“ [NSObject:AnyObject]”类型的值 - Cannot subscript a value of type '[NSObject : AnyObject]' with an index of type 'String' 不能下标[AnyObject]的值? 索引类型为Int - Cannot subscript a value of [AnyObject]? with an index of type Int 适用于iOS的Google登录:错误“无法使用”String“类型的索引下标类型'[String:AnyObject]'的值 - Google Sign-In for iOS: error “Cannot subscript a value of type '[String : AnyObject]' with an index of type 'String'” 迅速:无法下标[字典式]的值 <String, AnyObject> ],其索引类型为字符串 - swift: cannot subscript a value of type [Dictionary<String, AnyObject>] with an index of type string 无法下标anyobject类型的值 - Cannot subscript a value of type anyobject
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM