2015-06-06 58 views
1

當我運行模擬器時,我不斷收到這個致命錯誤,我不知道如何解決它。我創建與解析模擬Instagram的項目,由於某種原因,我得到一個致命的錯誤,並在線程1:swift:致命錯誤:意外地發現零,同時解開一個可選值(lldb),線程1

query.whereKey("follower", equalTo: PFUser.currentUser()!.objectId!) 

我知道這是一個常見的問題,因爲我經歷了很多問題看上去包含此致命錯誤。但是,我對Swift編程非常陌生,所以我感到困惑,希望有人能幫助我,謝謝!

import UIKit 
import Parse 

class TableViewController: UITableViewController { 

var usernames = [""] 
var userids = [""] 
var isFollowing = ["":false] 

override func viewDidLoad() { 
    super.viewDidLoad() 

    var query = PFUser.query() 

    query?.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in 

     if let users = objects { 

      self.usernames.removeAll(keepCapacity: true) 
      self.userids.removeAll(keepCapacity: true) 
      self.isFollowing.removeAll(keepCapacity: true) 

      for object in users { 

       if let user = object as? PFUser { 

        if user.objectId! != PFUser.currentUser()?.objectId { 

         self.usernames.append(user.username!) 
         self.userids.append(user.objectId!) 

         var query = PFQuery(className: "followers") 

         query.whereKey("follower", equalTo: PFUser.currentUser()!.objectId!) 
         query.whereKey("following", equalTo: user.objectId!) 

         query.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in 

          if let objects = objects { 

           if objects.count > 0 { 

            self.isFollowing[user.objectId!] = true 

           } else { 

            self.isFollowing[user.objectId!] = false 

           } 
          } 

          if self.isFollowing.count == self.usernames.count { 

           self.tableView.reloadData() 

          } 


         }) 



        } 
       } 

      } 



     } 



    }) 


} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

// MARK: - Table view data source 

override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    // #warning Potentially incomplete method implementation. 
    // Return the number of sections. 
    return 1 
} 

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    // #warning Incomplete method implementation. 
    // Return the number of rows in the section. 
    return usernames.count 
} 


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell 

    cell.textLabel?.text = usernames[indexPath.row] 

    let followedObjectId = userids[indexPath.row] 

    if isFollowing[followedObjectId] == true { 

     cell.accessoryType = UITableViewCellAccessoryType.Checkmark 

    } 


    return cell 
} 


override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 

    var cell:UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)! 

    let followedObjectId = userids[indexPath.row] 

    if isFollowing[followedObjectId] == false { 

     isFollowing[followedObjectId] = true 

     cell.accessoryType = UITableViewCellAccessoryType.Checkmark 

     var following = PFObject(className: "followers") 
     following["following"] = userids[indexPath.row] 
     following["follower"] = PFUser.currentUser()?.objectId 

     following.saveInBackground() 

    } else { 

     isFollowing[followedObjectId] = false 

     cell.accessoryType = UITableViewCellAccessoryType.None 

     var query = PFQuery(className: "followers") 

     query.whereKey("follower", equalTo: PFUser.currentUser()!.objectId!) 
     query.whereKey("following", equalTo: userids[indexPath.row]) 

     query.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in 

      if let objects = objects { 

       for object in objects { 

        object.deleteInBackground() 

       } 
      } 


     }) 



     } 

    } 
} 

回答

0

你可能沒有登錄任何用戶,以便PFUser.currentUser()將返回nil,當你強迫它解開,你不能解開一個零值創建錯誤。

首先檢查是否有用戶登錄使用:你是對的

if let user = PFUser.currentUser(){ 
    println("User logged, lets do this") 
    var query = PFQuery(className: "followers") 
    query.whereKey("follower", equalTo: PFUser.currentUser()!.objectId!) 
    query.whereKey("following", equalTo: userids[indexPath.row]) 
    query.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in 
     if let objects = objects { 
      for object in objects { 
       object.deleteInBackground() 
      } 
     } 
    }) 
} else { 
    println("No user logged, ops!") 
} 
+0

謝謝,我刪除了從我ViewController.Swift下面的代碼,所以我可以重新登錄,因爲代碼自動走進的tableView當我跑模擬器。一旦我登錄了我的所有數據在那裏。再次感謝FUNC viewDidAppear覆蓋(動畫:BOOL){ 如果PFUser.currentUser()= {零 self.performSegueWithIdentifier( 「登錄」,發件人:個體經營)! } } – morizvi23

+0

@DreJames高興聽到,請不要忘記檢查簽署的綠色支票,如果這個答案幫助你:) – Icaro

相關問題