gpt4 book ai didi

swift - 如何保存一个整数以在 Swift 中解析

转载 作者:行者123 更新时间:2023-11-28 07:01:12 25 4
gpt4 key购买 nike

我正在尝试将 integer 值保存到 Parse.com但我收到一个错误。我将 Parse 类中的对象设置为 Number

这是我的代码:

  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")
}
})

}

})

}

有人可以帮忙吗?

谢谢

最佳答案

发生此错误是因为您将 nil 转换为 Int。

我认为它有效:

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")
}
})

}
})
}

关于swift - 如何保存一个整数以在 Swift 中解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32002425/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com