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
.
Below is my function and the error I am getting:
Error I see :
Cannot subscript a value of [AnyObject]? with an index of type 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
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). 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
// 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.