我正在尝试将Number
值保存到Parse.com但我收到错误。我将Parse类中的对象设置为 if let currentUser = PFUser.currentUser() {
currentUser.fetchIfNeededInBackgroundWithBlock({ (foundUser: PFObject?, error: NSError?) -> Void in
// Get and update score
if foundUser != nil {
let score = foundUser!["score"] as! Int
let points = 100 + score
foundUser!["score"] = points
foundUser?.saveInBackgroundWithBlock({ (succeeded: Bool, error: NSError?) -> Void in
if succeeded {
println("score added to user")
}
})
}
})
}
。
这是我的代码:
{{1}}
有人可以帮忙吗?
由于
答案 0 :(得分:1)
发生此错误是因为您向Int。
投了一个nil我认为它有效:
if let currentUser = PFUser.currentUser() {
currentUser.fetchIfNeededInBackgroundWithBlock({ (foundUser: PFObject?, error: NSError?) -> Void in
// Get and update score
if let foundUser = foundUser {
if let score = foundUser["score"] as? Int {
foundUser["score"] = 100 + score
} else {
foundUser["score"] = 0
}
foundUser.saveInBackgroundWithBlock({ (succeeded: Bool, error: NSError?) -> Void in
if succeeded {
println("score added to user")
}
})
}
})
}
答案 1 :(得分:0)
我认为您应该以这种方式将foundUser!["score"]
转换为Int
:
let score = foundUser?["score"].integerValue
修改强>
if let currentUser = PFUser.currentUser() {
currentUser.fetchIfNeededInBackgroundWithBlock({ (foundUser: PFObject?, error: NSError?) -> Void in
// Get and update score
if foundUser != nil {
if let score = foundUser?["score"].integerValue {
let points = 100 + score
foundUser!["score"] = points
foundUser?.saveInBackgroundWithBlock({ (succeeded: Bool, error: NSError?) -> Void in
if succeeded {
println("score added to user")
}
})
}
}
})
}
希望它会有所帮助。