使用以下Swift代碼,我需要輸出5/hhhh的值。首先,在「var hhhh:Int = 0」處將hhhh設置爲零,然後在「getObjectInBackgroundWithId」塊內,hhhh的值由「hhhh = appleCount [」count「]更新爲!Int」只是從Parse中查詢。接下來,我需要以某種方式在「getObjectInBackgroundWithId」塊外部訪問hhhh的更新值,並打印出下面的值「print(5/hhhh)」,但是hhhh的值在這一行仍然爲零,我得到了NAN這個操作。 osteven好心給我爲什麼HHHH的價值在此時此地零一些提示:如何訪問Parse的getObjectInBackgroundWithId塊之外的變量值
Local and Global variables in Swift
但是,我還沒有想出一個辦法HHHH的更新值以某種方式轉移到「getObjectInBackgroundWithId」之外在「print(5/hhhh)」處使用該值,而不是零。如何使任何幫助或提示恰巧高度讚賞
import UIKit
import Parse
class NewsPageViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad(
var hhhh:Int = 0
var tttt:Int = 0
var cccc:Int = 1
if cccc == 1 {
var query = PFQuery(className: "Count")
query.getObjectInBackgroundWithId("RhC25gVjZm", block: { (object: PFObject?, error: NSError?) -> Void in
if error != nil {
print(error)
} else if let appleCount = object {
appleCount["count"] = appleCount["count"] as! Int + 1
hhhh = appleCount["count"] as! Int
appleCount.saveInBackground()
}
})
}
print(5/hhhh)
}
非常通過和解釋MHomer,非常感謝回答。現在,我對發生的事情有了更好的認識。我還添加了一些筆記,我在過去的幾個小時裏瞭解我的問題。所以,現在,我已經解決了這個問題,這要感謝你們。 – NewbieWantsToMaster