我有一个用户搜索学校,然后点击他们想要的那个。在cellForRowAtIndexPath中,我查询所选择的学校并获取其objectId。然后我将objectId设置为指针," school_name"在PFUser.currentUser中。指针指向" Schools"列出所有学校的班级。我收到的错误是,"密钥school_name的无效类型,预期*学校,但得到字符串(代码:111,版本:1.12.0)"。因为objectId是一个字符串,所以我似乎无法绕过它。我甚至可以打印出对象正确的对象。
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
let spinningActivity = MBProgressHUD.showHUDAddedTo(self.view, animated: true)
spinningActivity.labelText = "Just a Moment"
spinningActivity.detailsLabelText = "Adding Details"
let schoolIndexPath = tableView.indexPathForSelectedRow!
let selectedCell = tableView.cellForRowAtIndexPath(schoolIndexPath)! as UITableViewCell
let school : String = String(selectedCell.textLabel!.text!)
let schoolObjectIdQuery = PFQuery(className:"Schools")
schoolObjectIdQuery.whereKey("school_name", matchesRegex: school)
let query = PFQuery.orQueryWithSubqueries([schoolObjectIdQuery])
if reachabilityStatus == kNOTREACHABLE {
spinningActivity.hide(true)
self.displayAlert("No Internet Connection", message: "Please connect to the internet before continuing.")
} else {
query.findObjectsInBackgroundWithBlock {
(results: [PFObject]?, error: NSError?) -> Void in
if error != nil {
self.displayAlert("Error", message: error!.localizedDescription)
}
if let objects = results {
self.searchResults.removeAll(keepCapacity: false)
for object in objects {
let schoolObjectId = object.objectId!
let addDetails : PFUser = PFUser.currentUser()!
// let schoolObjectIdFormatted = PFObject(withoutDataWithClassName: "school_name", objectId: schoolObjectId)
addDetails.setObject(schoolObjectId, forKey: "school_name")
print(school)
print(schoolObjectId)
addDetails.saveInBackgroundWithBlock({ (success:Bool, error:NSError?) -> Void in
if error != nil {
spinningActivity.hide(true)
self.displayAlert("Error", message: error!.localizedDescription)
}
if success {
spinningActivity.hide(true)
self.dismissViewControllerAnimated(true, completion: nil)
print("Success")
}
})
}
}
}
}
}
答案 0 :(得分:0)
因为您正在尝试使用指针...不是字符串。您需要保存整个学校对象,而不是保存objectId。
答案 1 :(得分:0)
我认为您的代码存在更多错误。为什么你的每个下载对象都有addDetails["school_id"] = PFObject(withoutDataWithClassName: "School", objectId: "\(schoolObjectId)")
saveInBackGround。
如果你想存储指针,语法就是这样
pFirst
school_id是Parse DB中的一个列名,它是类School的指针