2015-05-29 103 views
0

在登錄應用程序中解開一個可選值(lldb)我試圖使登錄窗體作爲主要的初始屏幕,但我不能,一直打開用戶列表表。我沒有關於Parse的任何數據,但我有所有需要的類。它看起來像是以匿名用戶或其他方式登錄的,現在它出現很多錯誤,因爲沒有適合未知用戶的代碼?有人可以幫我嗎?致命錯誤:意外地發現零,而在解包

// 
// FeedTableViewController.swift 
// ParseStarterProject 
// 
// Created by Rob Percival on 19/05/2015. 
// Copyright (c) 2015 Parse. All rights reserved. 
// 

import UIKit 
import Parse 

class FeedTableViewController: UITableViewController { 

    var messages = [String]() 
    var usernames = [String]() 
    var imageFiles = [PFFile]() 
    var users = [String: String]() 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     var query = PFUser.query() 

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

      if let users = objects { 

       self.messages.removeAll(keepCapacity: true) 
       self.users.removeAll(keepCapacity: true) 
       self.imageFiles.removeAll(keepCapacity: true) 
       self.usernames.removeAll(keepCapacity: true) 

       for object in users { 

        if let user = object as? PFUser { 

         self.users[user.objectId!] = user.username! 

        } 
       } 
      } 


      var getFollowedUsersQuery = PFQuery(className: "followers") 

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

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

       if let objects = objects { 

        for object in objects { 

         var followedUser = object["following"] as! String 

         var query = PFQuery(className: "Post") 

         query.whereKey("userId", equalTo: followedUser) 

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

          if let objects = objects { 

           for object in objects { 

            self.messages.append(object["message"] as! String) 

            self.imageFiles.append(object["imageFile"] as! PFFile) 

            self.usernames.append(self.users[object["userId"] as! String]!) 

            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 myCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! cell 

     imageFiles[indexPath.row].getDataInBackgroundWithBlock { (data, error) -> Void in 

      if let downloadedImage = UIImage(data: data!) { 

       myCell.postedImage.image = downloadedImage 

      } 

     } 



     myCell.username.text = usernames[indexPath.row] 

     myCell.message.text = messages[indexPath.row] 

     return myCell 
    } 


    /* 
    // Override to support conditional editing of the table view. 
    override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool { 
    // Return NO if you do not want the specified item to be editable. 
    return true 
    } 
    */ 

    /* 
    // Override to support editing the table view. 
    override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { 
    if editingStyle == .Delete { 
    // Delete the row from the data source 
    tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) 
    } else if editingStyle == .Insert { 
    // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view 
    } 
    } 
    */ 

    /* 
    // Override to support rearranging the table view. 
    override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) { 

    } 
    */ 

    /* 
    // Override to support conditional rearranging of the table view. 
    override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool { 
    // Return NO if you do not want the item to be re-orderable. 
    return true 
    } 
    */ 

    /* 
    // MARK: - Navigation 

    // In a storyboard-based application, you will often want to do a little preparation before navigation 
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
    // Get the new view controller using [segue destinationViewController]. 
    // Pass the selected object to the new view controller. 
    } 
    */ 

} 
+0

中的錯誤發生什麼線? – Icaro

+0

getFollowedUsersQuery.whereKey(「follower」,equalTo:PFUser.currentUser()!. objectId!) –

回答

0

A PFUser的對象ID未存儲在設備上。你需要取它。以下是Objective-C,但它應該很容易轉換爲swift。

PFQuery *query = [PFUser query]; 
[query whereKey:@"username" equalTo:[[PFUser currentUser] objectForKey:@"username"]]; 
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { 
    if (!error) { 
     if (objects.count) { 
      for (PFObject *object in objects) { // just to be safe, but there should only be one. 
       NSLog(@"Object ID: %@", object.objectId); 
      } 
     } 
    } 
} 

在斯威夫特(因爲你禮貌地問)

var query = PFUser.query() 
query.whereKey("username", equalTo:PFUser.currentUser().objectForKey("username")) 
query.findObjectsInBackgroundWithBlock({(objects:NSArray, error:NSError) in 
    if error == nil { 
     if objects.count { 
      for var object in objects { 
       // here you can get object.objectID 
      } 
     } 
    } 
}) 
+0

請問您可以將它轉換爲swift嗎?確切的代碼。我試圖通過這個事情已經好幾天了..謝謝 –

+0

你應該在學習Swift的時候知道Objective-C。我會盡我所能,但相信自動完成,因爲我在沒有編譯器的情況下進行輸入。 – Schemetrical

+0

我需要複製它而不是代碼錯誤的地方? –

相關問題