将解析指针设置为 Class 除了 PFUser

Setting Parse Pointer to Class Other than PFUser

我正在尝试设置从 "Classes" 中的对象指向 "Schools" 中的对象的指针。当我这样做时,出现此错误:

invalid type for key school, expected *Schools, but got string (Code: 111, Version: 1.12.0)

我知道错误是说它需要 class、"Schools" 的指针,但它却得到了一个字符串。这是我的代码:

let schoolObjectIdQuery = PFQuery(className:"Schools")
        schoolObjectIdQuery.whereKey("school_name", equalTo: "\(PFUser.currentUser()!.objectForKey("school_name")!)")

        let query = PFQuery.orQueryWithSubqueries([schoolObjectIdQuery])

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

            if let objects = results {

                for object in objects {

                    let schoolObjectId = object.objectId!

                    //////////////////

                    let classInfo = PFObject(className: "Classes")

                    classInfo.setObject(className!, forKey: "class_name")
                    classInfo.setObject(teacherName!, forKey: "teacher_name")
                    classInfo["school"] = schoolObjectId

                    classInfo.saveInBackgroundWithBlock({ (success:Bool, error:NSError?) -> Void in

                        if error != nil {

                            spinningActivity.hideAnimated(true)

                            self.displayAlert("Error", message: error!.localizedDescription)

                            print(error?.localizedDescription)        
                        }
                        else if success {

                            spinningActivity.hideAnimated(true)

                            self.dismissViewControllerAnimated(true, completion: nil)        
                        }
                        else {

                            self.displayAlert("Something Went Wrong", message: "Please try again")               
                        }
                    })  
                }  
            }
        })

上面的代码正在查询正确的 "Schools" 对象的 objectId,然后将数据设置到另一个 class、"Classes",为此我需要 [=] 中的 objectId 26=] 对象。 * 我知道我的错误处理还没有正确设置

如果我用 classInfo["school"] = PFUser.currentUser() 替换 classInfo["school"] = schoolObjectId ,指针将毫无问题地发送到 Parse。如何将 class "Schools" 中指向 "school_name" 的指针设置为指针而不是字符串?

在此先感谢您的帮助!

Parse 中的指针不仅仅是 objectId。您必须反转 ParseObject,而不仅仅是 objectId。

PFUser.currentUser() returns 用户解析对象。这就是它起作用的原因。

试试这个:

classInfo["school"] = object