2015-12-14 54 views
0

使用以下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) 

}

回答

1

你已經給出的答案解釋的問題以及在我看來,我會嘗試改寫這個問題來幫助你瞭解如何解決它。

getObjectInBackgroundWithId函數在與當前正在運行的print(5/hhhh)語句不同的時間運行一個塊。

因此值'hhhh'正在更新,但是您在getObjectInBackgroundWithId中的塊有時間運行和更新之前嘗試調用print語句中的hhhh值。

換句話說,

print(5/hhhh) 

正在執行之前的代碼塊(由於後臺線程的性質)正在發生,

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

利用HHHH可變一次的方式你知道它已被更新,將創建一個函數來使用(在你的情況下打印)變量並在塊內調用它,如下所示:

示例代碼:

func doSomethingWithHHHH(){ 
    print(5/hhhh) 
} 

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 
    //HERE YOU CALL ON THE FUNCTION TO USE THE VARIABLE 
      doSomethingWithHHHH() 
      appleCount.saveInBackground() 
     } 
    }) 

在這個例子中,print語句應該工作,因爲變量的函數之前,塊被更新被稱爲使用「HHHH」變量。

+0

非常通過和解釋MHomer,非常感謝回答。現在,我對發生的事情有了更好的認識。我還添加了一些筆記,我在過去的幾個小時裏瞭解我的問題。所以,現在,我已經解決了這個問題,這要感謝你們。 – NewbieWantsToMaster

0

除了MHomer的反應,我只想補充一點,我的問題的一個主要部分來了,因爲我試圖對我試圖保存在Parse中的對象進行一些數學運算。所以,我還認爲我可以創建一個數組,將該對象保存在該數組中,然後訪問該鍵並更新這些值。這裏是我現在使用的示例代碼。它對保存在Parse中的兩個不同對象執行一些數學操作,並更新屏幕上的標籤文本。爲了訪問Parse中的兩個對象並更新它們,我使用了一個數組。

希望這裏的答案將有助於未來的人,因爲StackOverFlow的傑出人物現在正在幫助我。 和平!

var hhhh : [Int] = [] 
@IBOutlet weak var jPercent: UILabel! 

@IBOutlet weak var yPercent: UILabel! 

override func viewDidLoad() { 
    super.viewDidLoad() 

    var query = PFQuery(className: "Count") 
    if cccc == 1 { 
     query.getObjectInBackgroundWithId("DcU9tdRdnl", block: { (object: PFObject?, error: NSError?) -> Void in 
      if error != nil { 
       print(error) 
      } else if let jCount = object { 
       jCount["count"] = jCount["count"] as! Int + 1 
       jCount.saveInBackground() 

      } 
     }) 
    } else if cccc == 2 { 
     query.getObjectInBackgroundWithId("5Bq4HJbFa3", block: { (object: PFObject?, error: NSError?) -> Void in 
      if error != nil { 
       print(error) 
      } else if let yCount = object { 
       yCount["count"] = yCount["count"] as! Int + 1 
       yCount.saveInBackground() 
      } 
     }) 
    } 

    //shouldn't use same query for findObjectsInBackgroundWithBlock and getObjectInBackgroundWithId otherwise you'll get a runtime error 
    var query2 = PFQuery(className: "Count") 
    query2.findObjectsInBackgroundWithBlock { (objects, error) -> Void in 
     if let users = objects { 
      for object in users { 
       if let user = object["count"] as? Int { 
        self.hhhh.append(user) 
       } 

      } 

     } 
     var gggg = 100*Float(self.hhhh[0])/(Float(self.hhhh[0]+self.hhhh[1])) 
     self.yPercent.text = String(format: "%.1f", gggg) + "%" 
     self.jPercent.text = String(format: "%.1f", 100 - gggg) + "%" 
     print(self.hhhh[0]) 
    } 
} 
相關問題