【问题标题】:Make a Query with a Pointer in Parse and Swift在 Parse 和 Swift 中使用指针进行查询
【发布时间】:2015-10-06 21:11:02
【问题描述】:

我正在尝试使用 Parse 中的指针进行查询。

基本上我有两个类"commentsTable""_User",我想在确定的帖子上从"commentsTable"类中获取用户的评论,然后从类中获取usernameprofile_pic "_User"

_用户类

cmetsTable 类

    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: teste@teste.com
By user: mmachado

在我的应用程序中,我在 TableView 中显示此信息,我成功地可以在 func cellForRowAtIndexPath 中显示查询的结果:

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

但我无法返回我的其他类的值,_User

我想我误解了一些概念,但此时我不知道什么概念,有什么想法?

谢谢。

【问题讨论】:

    标签: ios swift parse-platform


    【解决方案1】:

    通过使用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

    【讨论】:

    • @Rusell 完美的解释,我已经知道如何加载 PFFile 和网站 medium.com '我的问题真的是关于如何从我的其他课程中获取内容,你的解释解决了我的疑惑,也感谢建议将我的用户指针的名称从username更改为user
    • 我很快就会删除这条评论,但是你们能帮我解决这个问题吗? stackoverflow.com/questions/33606788/…我觉得你很懂管理,但是你的回答不适用于我的问题(至少,我不明白怎么做)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多