2014-11-22 43 views
0

我有一個查詢需要訪問我的分析數據控制器上特定行中的數據。每行數據都有不同的對象ID,而且我似乎需要爲每行數據寫一個全新的函數。舉例來說,這裏的功能:使用不同的分析對象ID重複查詢時遇到問題

@IBAction func addVote1(sender: AnyObject) { 
    var query = PFQuery(className: "VoteCount") 
    query.getObjectInBackgroundWithId("BiEM17uUYT") { 
     (voteCount1: PFObject!, error: NSError!) -> Void in 
     if error != nil { 
      NSLog("%@", error) 
     } else { 
      voteCount1.incrementKey("votes") 
      voteCount1.saveInBackgroundWithTarget(nil, selector: nil) 
      let votes = voteCount1["votes"] as Int 
      let votes2 = voteCount1["votes2"] as Int 
      self.pollResults1.text = "\(votes)" 
     } 

     } 
    } 

的事情是,我有另外一個查詢按鈕可以調用是完全相同的功能和查詢只在「getObjectInBackgroundWithId」不同的ID如下:

@IBAction func addVote1(sender: AnyObject) { 
    var query = PFQuery(className: "VoteCount") 
    query.getObjectInBackgroundWithId("TtKGatVCi9") { 
     (voteCount1: PFObject!, error: NSError!) -> Void in 
     if error != nil { 
      NSLog("%@", error) 
     } else { 
      voteCount1.incrementKey("votes") 
      voteCount1.saveInBackgroundWithTarget(nil, selector: nil) 
      let votes = voteCount1["votes"] as Int 
      let votes2 = voteCount1["votes2"] as Int 
      self.pollResults1.text = "\(votes)" 
     } 

     } 
    } 

我該如何做到這一點,以便我只需要編寫一次代碼並只需要使用隨機對象ID訪問數據?我在我的viewDidLoad方法到目前爲止試過,但只得到錯誤信息:

 let array = ["BiEM17uUYT", "TtKGatVCi9"] 
    let randomIndex = Int(arc4random_uniform(UInt32(array.count))) 
    var objectId = array[randomIndex] 

    var query = PFQuery(className: "VoteCount") 
    query.getObjectInBackgroundWithId("objectId") { 
     (voteCount1: PFObject!, error: NSError!) -> Void in 
     if error != nil { 
      NSLog("%@", error) 
     } else { 
      voteCount1.incrementKey("votes") 
      voteCount1.saveInBackgroundWithTarget(nil, selector: nil) 
      let votes = voteCount1["votes"] as Int 
      let votes2 = voteCount1["votes2"] as Int 
      self.pollResults1.text = "\(votes)" 
     } 
    } 

它是什麼我做錯了,而且我怎麼可能只訪問一個隨機的ObjectId數組中?

回答

0

您正在將值爲「objectId」的字符串傳遞給getObjectInBackgroundWithId而不是objectId的值。

變化:

query.getObjectInBackgroundWithId("objectId") { 

到:

query.getObjectInBackgroundWithId(objectId) { 
+0

你試試這個?如果它不起作用,你看到了什麼症狀? – vacawama 2014-11-24 14:05:26