2015-10-06 28 views
2

我試圖在解析中使用指針進行查詢。在解析和Swift中使用指針進行查詢

基本上我有兩個班"commentsTable""_User",我想從"commentsTable"類所確定後,用戶的評論,然後拿到usernameprofile_pic從類"_User"

_user類 enter image description here

commentsTable類 enter image description here

func loadAndShowComments(){ 
    let query2 = PFQuery(className: "commentsTable") 
    query2.orderByDescending("createdAt") 
    query2.whereKey("newsColumns", equalTo: printteste!) 
    query2.includeKey("username") 

    query2.findObjectsInBackgroundWithBlock { 
     (objects: [PFObject]?, error: NSError?) -> Void in 

     if let objects = objects as [PFObject]? { 

      for object in objects { 

       print(object["commentColumn"]) 

      } 

     } 


     for cardset in objects! { 
      var lesson = cardset["username"] as! PFObject 
      var name = lesson["username"] as! String 
      print("By user: \(name)") 
     } 

我能看到的查詢,我打印出結果的,我有以下的輸出:

This is a post! 
This is a test post! 
By user: [email protected] 
By user: mmachado 

而且在我的應用我顯示的TableView這裏面的信息,我能成功顯示在FUNC cellForRowAtIndexPath對查詢結果:

if let usuarioComentario = object?["commentColumn"] as? String { 
     cell?.usuarioComentario?.text = usuarioComentario 
    } 

但我沒能回到我的其他類的值,_User

我想我誤解了一些概念,但在這一點上我不知道什麼概念,有什麼想法?

謝謝。

回答

3

使用query2.includeKey("username")您已經檢索每個commentsTable對象關聯的User數據的所有

您可以通過以下方式訪問相關的User數據。

if let commentUser = object["username"] as? PFUser { 
    let name = commentUser["username"] as! String 
    let profilePicture = commentUser["profile_pic"] as! PFFile 
} 

如果您尚未使用,則需要將查詢結果存儲到數組中供以後使用。如果您使用的是Parse提供的PFQueryTableViewController,則將通過執行queryForTable()方法爲您處理,並將結果自動存儲在名爲objects的字典數組中。

還值得注意的是,您將不得不加載PFFile,因爲它們不包含在查詢結果中。您需要將PFFile分配給PFImageView,然後撥打loadInBackground。看下面的例子。

let imageView = PFImageView() 

// Set placeholder image 
imageView.image = UIImage(named: "placeholder") 

// Set remote image 
imageView.file = profilePicture 

// Once the download completes, the remote image will be displayed 
imageView.loadInBackground { (image: UIImage?, error: NSError?) -> Void in 
    if (error != nil) { 
     // Log details of the failure 
     println("Error: \(error!) \(error!.userInfo!)")    
    } else { 
     println("image loaded") 
    } 
} 

最後,我建議你從「用戶名」爲「用戶」,所以與用戶類的用戶名字段沒有混亂中commentsTable改變User指針的名稱。 Here's a link to a great tutorial which you may also find helpful

+0

@Rusell完美的解釋,我已經知道如何加載PFFile和網站[medium.com](http://medium.com)'我的問題確實是關於如何獲取內容我的其他班級,你的解釋解決了我的疑惑,也感謝您的建議,改變我的用戶指針的名字,從'用戶名'到'用戶' –

+0

我很快會刪除這條評論,但你能幫我這個?http://stackoverflow.com/questions/33606788/swift-how-to-get-parses-many-users-name-and-photo我想你非常瞭解如何管理,但你的答案不適用於我的問題(至少,我不明白如何) – biggreentree