2016-03-16 44 views
0

我試圖將「類」中的對象設置爲「學校」中的對象。當我這樣做,我得到這個錯誤:將分析指針設置爲PFUser以外的類

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

我知道錯誤是說,它預計類的指針,「學校」,但它有一個字符串代替。這裏是我的代碼:

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")    
         } 
        }) 
       } 
      } 
     }) 

上面的代碼查詢正確的「學校」對象的OBJECTID數據,然後設置到另一個類,「類」,爲此,我需要的OBJECTID從「學校」目的。 *我知道我的錯誤處理還沒有正確設置

如果我用classInfo["school"] = PFUser.currentUser()替換classInfo["school"] = schoolObjectId指針發送到Parse沒有問題。我如何在類「Schools」中將指針設置爲「school_name」作爲指針而不是字符串?

在此先感謝您的幫助!

回答

1

解析中的指針不僅僅是objectId。您必須反轉ParseObject,而不僅僅是objectId。

PFUser.currentUser()返回用戶分析對象。那就是它的原因。

試試這個:

classInfo["school"] = object 
+0

謝謝!這正是我發佈後想到的。我稍後再嘗試一下,並將其標記爲正確的答案 –

相關問題