[英]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.