我想知道一个客户的名字,他是一名维修赛的主人。
这是我的密码。
为了保存数据,我要这样做:
self.vitrine["username"] = PFUser.currentUser()?.username
self.vitrine["name"] = nameTextField.text as String
var relation = self.vitrine.relationForKey("client")
relation.addObject(self.client)
self.vitrine.saveEventually { (success, error) -> Void in
if(error == nil){
}else{
println(error?.userInfo)
}
self.fetchAllVitrines()
self.navigationController?.popToRootViewControllerAnimated(true)
}
}而且它是有效的。在“分析”中,我可以看到这种关系在起作用。
我正在尝试访问关系数据,这样做:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("vitrineCell", forIndexPath: indexPath) as! VitrineTableViewCell
var object: PFObject = self.vitrineObjects.objectAtIndex(indexPath.row) as! PFObject
cell.nomeLabel.text = object["name"] as? String
let x: PFRelation = object["client"] as! PFRelation
// cell.clientNameTextView.text = object["client"]["name"] as String
return cell
}但是,当我在client列中记录数据时,显示的是:
<PFRelation: 0x7b7f1390, 0x7b7cfdc0.client -> Client>
拜托有人帮我。我用了两天。我读了三遍“分析博士”。我没有办法做到这一点。
你好,迪奥戈·阿马拉尔
好吧,我把代码加进去:
query!.getFirstObjectInBackgroundWithBlock {
(object: PFObject?, error: NSError?) -> Void in
if error != nil || object == nil {
println("The getFirstObject request failed.")
} else {
println(object)
cell.clientNameTextView.text = object["name"] as? String
}
}但是这句话:cell.clientNameTextView.text = object["name"] as? String抛出了一个错误。“不能将'String?‘类型的值赋值给’String‘类型的值!”
我已经试过了
cell.clientNameTextView.text = object["name"] as! String cell.clientNameTextView.text = object["name"] as String cell.clientNameTextView.text = object["name"] as? String
我怎么才能解决这个问题?
发布于 2015-07-21 06:13:28
如果使用的是关系(而不是指针),则需要注意关系存储其目标对象的数组。所以,当你执行
var relation = self.vitrine.relationForKey("client")
relation.addObject(self.client) 您将self.client添加到client的数组中。如果一个client只能有一个所有者,并且应该存储在客户端字段中,那么您可能希望使用一个指针而不是一个关系。
因为有了这个数组,编写的代码就不能工作了。您需要从vitrine对象中获取关系,查询它,从您想要的数组中提取元素,然后可以使用它。
let x: PFRelation = object["client"] as! PFRelation
let query = x.query()
// Lets say that you are only interested in the first element in your array...
query.getFirstObjectInBackgroundWithBlock { first, error in
// Should do error checking here
cell.clientNameTextView.text = first!.objectForKey("name") as? String
}这种方法也有点低效。您应该使用某种形式的缓存,或者在理想情况下,确定是否确实需要使用关系,或者指针是否足够。如果指针可以执行,则还可以包括它通过includeKey方法在PFQuery上运行原始查询时指向的数据。您不能在关系上使用includeKey。
https://stackoverflow.com/questions/31526420
复制相似问题