PFQuery:无法从解析数组中获取objectId

时间:2015-01-17 13:07:56

标签: ios arrays swift parse-platform pfquery

我正在为社交墙做一个类似的功能。 当currentUser执行此操作时,它会将messageId从消息添加到User类中的Array列:

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

这是您在“喜欢”数组中获得的内容:["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类型进行比较查询。我已经设置了Exceptional断点并且来到了这个:+[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 ()

搜索后我仍然不太明白这意味着什么。似乎查询错了。如何访问“喜欢”的数组并进行比较?

1 个答案:

答案 0 :(得分:3)

我的第一个猜测是,问题是:

message.objectForKey("objectId")

语法objectForKey适用于您创建的PFObjects的那些列,但objectId是由Parse创建的。所以只需使用

message.objectId

,因为objectId存储为PFObject的属性。