PFQuery:无法从解析数组中获取 objectId
PFQuery: can't get objectId from parse array
我正在为社交墙制作类似的功能。
当 currentUser 执行此操作时,它会将消息中的 objectId 添加到 User class:
中的数组列
func likeBtnClick(sender: AnyObject){
let senderbutton:UIButton = sender as UIButton
println("current row is = \(senderbutton.tag)")
let tempObject:PFObject = ImageTimeLineData.objectAtIndex(senderbutton.tag) as PFObject
println("\(tempObject.objectId)")
PFUser.currentUser().addUniqueObject(tempObject.objectId, forKey: "liked")
PFUser.currentUser().saveInBackground()
}
这就是您将在 "liked" 数组中得到的内容:["q6begjrFE4","s63ehjxFA1"]
这一切都很好。
现在我想从 cellForRowAtIndexPath
中的消息中检索喜欢的数量并在按钮中显示喜欢的数量:
.....
let message:PFObject = self.ImageTimeLineData.objectAtIndex(indexPath!.row) as PFObject
.....
var findLikes:PFQuery = PFUser.query()
findLikes.whereKey("liked", equalTo: message.objectForKey("objectId"))
findLikes.findObjectsInBackgroundWithBlock{
(objects:[AnyObject]!, error:NSError!) -> Void in
if error == nil{
let liked:NSArray = objects as NSArray
println(liked)
println(liked.count)
cell.likedButton.setTitle("\(liked.count)", forState: UIControlState.Normal)
}
}
.....
cell.likedButton.tag = indexPath!.row
cell.likedButton.addTarget(self, action: "likeBtnClick:", forControlEvents: UIControlEvents.TouchUpInside)
return cell
这会使应用程序崩溃,表示它无法对类型 null 进行比较查询。我已经设置了异常断点并来到这里:+[PFInternalUtils assertValidClassForQuery:]
。它在这条线上崩溃:findLikes.whereKey("liked", equalTo: message.objectForKey("objectId"))
行。
#0 0x01833a6b in objc_exception_throw ()
#1 0x01bae86d in +[NSException raise:format:] ()
#2 0x00197f30 in +[PFInternalUtils assertValidClassForQuery:] at /Users/nlutsenko/src/parse/ios-client/Parse/Internal/PFInternalUtils.m:368
#3 0x001679d3 in -[PFQuery whereKey:equalTo:] at /Users/nlutsenko/src/parse/ios-client/Parse/PFQuery.m:195
#4 0x000cce00 in TongerenApp.ImageTimeLineTableViewController.tableView (TongerenApp.ImageTimeLineTableViewController)(Swift.Optional<ObjectiveC.UITableView>, cellForRowAtIndexPath : Swift.Optional<ObjectiveC.NSIndexPath>) -> ObjectiveC.UITableViewCell at /Users/Dax/Desktop/TongerenApp/TongerenApp/ImageTimeLineTableViewController.swift:130
#5 0x000cf143 in @objc TongerenApp.ImageTimeLineTableViewController.tableView (TongerenApp.ImageTimeLineTableViewController)(Swift.Optional<ObjectiveC.UITableView>, cellForRowAtIndexPath : Swift.Optional<ObjectiveC.NSIndexPath>) -> ObjectiveC.UITableViewCell ()
#6 0x022881bc in -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] ()
#7 0x0228829e in -[UITableView _createPreparedCellForGlobalRow:willDisplay:] ()
#8 0x02261a6b in -[UITableView _updateVisibleCellsNow:isRecursive:] ()
#9 0x0227c3d1 in -[UITableView layoutSubviews] ()
#10 0x021f1dd1 in -[UIView(CALayerDelegate) layoutSublayersOfLayer:] ()
#11 0x01849771 in -[NSObject performSelector:withObject:] ()
#12 0x0074628f in -[CALayer layoutSublayers] ()
#13 0x0073a115 in CA::Layer::layout_if_needed(CA::Transaction*) ()
#14 0x00739f70 in CA::Layer::layout_and_display_if_needed(CA::Transaction*) ()
#15 0x006983c6 in CA::Context::commit_transaction(CA::Transaction*) ()
#16 0x0069978c in CA::Transaction::commit() ()
#17 0x00699e58 in CA::Transaction::observer_callback(__CFRunLoopObserver*, unsigned long, void*) ()
#18 0x01ad19de in __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ ()
#19 0x01ad1920 in __CFRunLoopDoObservers ()
#20 0x01ac735a in __CFRunLoopRun ()
#21 0x01ac6bcb in CFRunLoopRunSpecific ()
#22 0x01ac69fb in CFRunLoopRunInMode ()
#23 0x052da24f in GSEventRunModal ()
#24 0x052da08c in GSEventRun ()
#25 0x021668b6 in UIApplicationMain ()
#26 0x0010ff9e in top_level_code at /Users/Dax/Desktop/TongerenApp/TongerenApp/AppDelegate.swift:12
#27 0x0011008b in main ()
#28 0x03885ac9 in start ()
查了下还是不太明白这是什么意思。似乎查询是错误的。如何访问 "liked" 数组并进行比较?
我的第一个猜测是,问题是:
message.objectForKey("objectId")
语法 objectForKey 适用于您创建的 PFObjects 的那些列,但 objectId 是由 Parse 创建的。因此,只需使用
message.objectId
,因为 objectId 存储为 PFObject.
的 属性
我正在为社交墙制作类似的功能。 当 currentUser 执行此操作时,它会将消息中的 objectId 添加到 User class:
中的数组列func likeBtnClick(sender: AnyObject){
let senderbutton:UIButton = sender as UIButton
println("current row is = \(senderbutton.tag)")
let tempObject:PFObject = ImageTimeLineData.objectAtIndex(senderbutton.tag) as PFObject
println("\(tempObject.objectId)")
PFUser.currentUser().addUniqueObject(tempObject.objectId, forKey: "liked")
PFUser.currentUser().saveInBackground()
}
这就是您将在 "liked" 数组中得到的内容:["q6begjrFE4","s63ehjxFA1"]
这一切都很好。
现在我想从 cellForRowAtIndexPath
中的消息中检索喜欢的数量并在按钮中显示喜欢的数量:
.....
let message:PFObject = self.ImageTimeLineData.objectAtIndex(indexPath!.row) as PFObject
.....
var findLikes:PFQuery = PFUser.query()
findLikes.whereKey("liked", equalTo: message.objectForKey("objectId"))
findLikes.findObjectsInBackgroundWithBlock{
(objects:[AnyObject]!, error:NSError!) -> Void in
if error == nil{
let liked:NSArray = objects as NSArray
println(liked)
println(liked.count)
cell.likedButton.setTitle("\(liked.count)", forState: UIControlState.Normal)
}
}
.....
cell.likedButton.tag = indexPath!.row
cell.likedButton.addTarget(self, action: "likeBtnClick:", forControlEvents: UIControlEvents.TouchUpInside)
return cell
这会使应用程序崩溃,表示它无法对类型 null 进行比较查询。我已经设置了异常断点并来到这里:+[PFInternalUtils assertValidClassForQuery:]
。它在这条线上崩溃:findLikes.whereKey("liked", equalTo: message.objectForKey("objectId"))
行。
#0 0x01833a6b in objc_exception_throw ()
#1 0x01bae86d in +[NSException raise:format:] ()
#2 0x00197f30 in +[PFInternalUtils assertValidClassForQuery:] at /Users/nlutsenko/src/parse/ios-client/Parse/Internal/PFInternalUtils.m:368
#3 0x001679d3 in -[PFQuery whereKey:equalTo:] at /Users/nlutsenko/src/parse/ios-client/Parse/PFQuery.m:195
#4 0x000cce00 in TongerenApp.ImageTimeLineTableViewController.tableView (TongerenApp.ImageTimeLineTableViewController)(Swift.Optional<ObjectiveC.UITableView>, cellForRowAtIndexPath : Swift.Optional<ObjectiveC.NSIndexPath>) -> ObjectiveC.UITableViewCell at /Users/Dax/Desktop/TongerenApp/TongerenApp/ImageTimeLineTableViewController.swift:130
#5 0x000cf143 in @objc TongerenApp.ImageTimeLineTableViewController.tableView (TongerenApp.ImageTimeLineTableViewController)(Swift.Optional<ObjectiveC.UITableView>, cellForRowAtIndexPath : Swift.Optional<ObjectiveC.NSIndexPath>) -> ObjectiveC.UITableViewCell ()
#6 0x022881bc in -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] ()
#7 0x0228829e in -[UITableView _createPreparedCellForGlobalRow:willDisplay:] ()
#8 0x02261a6b in -[UITableView _updateVisibleCellsNow:isRecursive:] ()
#9 0x0227c3d1 in -[UITableView layoutSubviews] ()
#10 0x021f1dd1 in -[UIView(CALayerDelegate) layoutSublayersOfLayer:] ()
#11 0x01849771 in -[NSObject performSelector:withObject:] ()
#12 0x0074628f in -[CALayer layoutSublayers] ()
#13 0x0073a115 in CA::Layer::layout_if_needed(CA::Transaction*) ()
#14 0x00739f70 in CA::Layer::layout_and_display_if_needed(CA::Transaction*) ()
#15 0x006983c6 in CA::Context::commit_transaction(CA::Transaction*) ()
#16 0x0069978c in CA::Transaction::commit() ()
#17 0x00699e58 in CA::Transaction::observer_callback(__CFRunLoopObserver*, unsigned long, void*) ()
#18 0x01ad19de in __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ ()
#19 0x01ad1920 in __CFRunLoopDoObservers ()
#20 0x01ac735a in __CFRunLoopRun ()
#21 0x01ac6bcb in CFRunLoopRunSpecific ()
#22 0x01ac69fb in CFRunLoopRunInMode ()
#23 0x052da24f in GSEventRunModal ()
#24 0x052da08c in GSEventRun ()
#25 0x021668b6 in UIApplicationMain ()
#26 0x0010ff9e in top_level_code at /Users/Dax/Desktop/TongerenApp/TongerenApp/AppDelegate.swift:12
#27 0x0011008b in main ()
#28 0x03885ac9 in start ()
查了下还是不太明白这是什么意思。似乎查询是错误的。如何访问 "liked" 数组并进行比较?
我的第一个猜测是,问题是:
message.objectForKey("objectId")
语法 objectForKey 适用于您创建的 PFObjects 的那些列,但 objectId 是由 Parse 创建的。因此,只需使用
message.objectId
,因为 objectId 存储为 PFObject.
的 属性