2015-12-19 64 views
0
檢索信息

我想建立一個聊天應用程序,但我有這個代碼的問題:從parse

func loadData() { 

     let FindTimeLineData: PFQuery = PFQuery(className: "Message") 
     FindTimeLineData.findObjectsInBackgroundWithBlock { (objects: [AnyObject]!, NSError) -> Void in 

      self.MessagesArray = [String]() 

      for MessageObject in objects { 

       let messageText: String? = (MessageObject as! PFObject) ["Text"] as? String 

       if messageText != nil { 

        self.MessagesArray.append(messageText!) 

      } 
     } 
     } 
    } 

我需要檢索解析數據,但.findObjectsInBackgroundWithBlock方法告訴我,它不能將AnyObject類型的值轉換爲Void in。如何解決此問題?提前致謝。

+0

遺憾。你能幫我用這個代碼Eric嗎? –

+0

是的:可能重複的[不能調用'getDataInBackgroundWithBlock'與類型的參數列表'((NSData !, NSError!) - > Void)'](http://stackoverflow.com/questions/29761799/cannot-invoke- getdatainbackgroundwithwith-an-argument-list-of-type-ns)(請參閱接受的答案,方法簽名) – Moritz

+0

非常感謝你 –

回答

2

嘗試像這樣代替:

var query = PFQuery(className: "Message") 
query.findObjectsInBackgroundWithBlock { 
    (remoteObjects: [PFObject]?, error: NSError?) -> Void in 

    if error == nil { 
     print("Retrieved \(remoteObjects!.count) messages from server.") 
     self.MessagesArray = [String]()  // By convention, you should name this "messagesArray" instead, and initialize it outside this method 

     for messageObject in remoteObjects { 

      if let messageText: String? = messageObject["Text"] as? String { 
       self.messagesArray.append(messageText) 
      } 
     } 
    } else { 
     print("Error: \(error!) \(error!.userInfo)") 
    } 
} 

(不正確地校對,但是你應該能夠得到它,從這個工作)

+1

非常感謝 –

1

爲了記錄在案,有重複的地段這個問題的問題 - 我知道,因爲在將Parse代碼轉換爲Swift 2.1後,我遇到了同樣的問題。 因此,請在發佈問題之前多做一點研究。通常,SO甚至會在輸入時提示類似的問題...

至於答案,Parse API不會強制您在查詢的完成塊中將對象轉換爲AnyObject,因此它可看起來就像這樣:

query?.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in 
     if let messages = objects { 
      for message in messages { 

....等