簡體   English   中英

PFQuery:無法從解析數組中獲取objectId

[英]PFQuery: can't get objectId from parse array

我正在為社交牆做一個類似的功能。 當currentUser執行此操作時,它會將消息中的objectId添加到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 ()

搜索后我仍然不太明白這意味着什么。 似乎查詢錯了。 如何訪問“喜歡”的數組並進行比較?

我的第一個猜測是,問題是:

message.objectForKey("objectId")

語法objectForKey適用於您創建的PFObjects的那些列,但objectId是由Parse創建的。 所以只需使用

message.objectId

,因為objectId存儲為PFObject的屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM