ios - 将字符串转换为 PFObject.. 解析

标签 ios swift pointers parse-platform

我正在以这种方式检索所有用户详细信息..

var userIds = [String]()
var userNames = [String]()
var profilePics = [PFFile]()
var gender = [String]()


override func viewDidLoad() {
    super.viewDidLoad()

    var userQuery = PFUser.query()
        userQuery?.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in

            if let objects = objects {

                self.userIds.removeAll(keepCapacity: true)
                self.userNames.removeAll(keepCapacity: true)
                self.profilePics.removeAll(keepCapacity: true)

                for object in objects {

                    if let user = object as? PFUser {
                        if user.objectId !=  PFUser.currentUser()?.objectId {

                            self.userIds.append(object.objectId as String!)
                            self.userNames.append(object["fullName"] as! String!)
                            self.profilePics.append(object["profilePicture"] as! PFFile!)
                            self.gender.append(object["gender"] as! String!)

                        }


                    }

                }


            }


            self.tableView.reloadData()

        })

}

现在我在 myCell 中有“关注”按钮.. 以这种方式保存..

 @IBAction func followButtonTapped(sender: UIButton) {

    var followers:PFObject = PFObject(className: "Followers")
    followers["user"] = userIds[sender.tag] // Problem is here.. i want "user" column to be filled with userIds of users as Pointers not Strings. What should i do here??
    followers["follower"] = PFUser.currentUser()

    followers.saveInBackground()

}

因为我想与“Followers”类中的每个对象建立关系,这就是为什么我希望它们采用指针的形式。有什么建议吗??

最佳答案

如果您想创建指向另一个 PFObject(包括 PFUser)的指针,您将需要实际指向该对象。

我在您的查询中看到您没有将任何 PFUsers 添加到数组中,但您正在获取它们的对象 ID。

由于标签仅适用于 Int,因此您不能使用它来传递 String(Parse objectID 就是)。

您可以子类化 UIButton 并创建一个新属性来处理来自 parse 的 objectID,然后使用它来对 objectID 执行查询并保存指向该查询结果的指针。或者,您可以将第一个查询中的对象添加到本地数组,并使用按钮的标签获取您要指向的对象的索引。

更新

由于您拥有所需 PFuser 的 objectId,因此您可以获取它并更新您的关注者,如下所示:

        let getOjbectByIdQuery = PFQuery(className: "User")
    getOjbectByIdQuery.whereKey("objectId", equalTo: userIds[sender.tag])
    getOjbectByIdQuery.getFirstObjectInBackgroundWithBlock { (foundObject: PFObject?, error: NSError?) -> Void in

        if let object = foundObject {

            var followers:PFObject = PFObject(className: "Followers")
            followers["user"] = object
            followers["follower"] = PFUser.currentUser()
            follwers.saveInBackground()

        }
    }

关于ios - 将字符串转换为 PFObject.. 解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31735055/

相关文章:

json - Swift JSOn 解码错误 : Expected to decode Array<Any> but found a dictionary instead

c - 从c中的函数返回指针

c - 为什么使用 malloc 创建的指针进行赋值会导致段错误

c - 关于函数内部的自由指针有点令人困惑

ios - 如何在 Swift 中发送 POST 参数?

iOS WatchKit 和静态库类

iphone - 在条件中设置浮点值

swift - UILongPressGestureRecognizer 和 UIScreenEdgePanGestureRecognizer

ios - 区分accessoryView和Row on touch

ios - 标题 Collection View 中的分组数据