2015-01-17 25 views
1

我正在爲社交牆做類似的功能。 當currentUser做到這一點它就會從消息中的用戶類中添加OBJECTID到一個數組列:PFQuery:無法從解析數組中獲取objectId

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做一個比較查詢。我設置了特殊的斷點並出現在這裏:+[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() 

搜索後我還是不太明白這是什麼意思。似乎查詢是錯誤的。我如何訪問「喜歡」的數組並進行比較?

+0

顯示它崩潰的行和完整的異常消息和堆棧跟蹤 – Wain

回答

3

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

message.objectForKey("objectId") 

語法objectForKey的工作原理與PFObjects的那些列,您創建的,但對象ID被解析創建。因此,只需使用

message.objectId 

,因爲objectId是作爲PFObject的屬性存儲的。

+0

你是一個英雄,首先完成了這個,但是使用了錯誤的whereKey func。 – SwingerDinger

+0

我很高興能幫到你。 –