2016-04-14 47 views
0

我試圖從Parse下載一個文件,但我得到一個致命的錯誤:崩潰,它好像消息變量是零。我試圖排除故障,並且我知道有一個簡單的錯過我只是沒有看到。希望社區能夠提供幫助,因爲我是新來的迅速。遇到無可選使用解析

class InboxViewController: UITableViewController { 

    var messages: [PFObject] = [] 
    var selectedMessage: PFObject? 
    var moviePlayer: MPMoviePlayerController? 

    override func viewDidLoad() { 
    super.viewDidLoad() 
    self.moviePlayer = MPMoviePlayerController() 

    let currentUser = PFUser.currentUser() 
    if currentUser != nil { 
     // Do stuff with the user 
     print("Current user: \(currentUser!.username)") 
    } else { 
    } 
} 

override func viewWillAppear(animated: Bool) { 
    super.viewWillAppear(animated) 

    let query = PFQuery(className: "Messages") 
query.whereKey("recipientIds", equalTo: PFUser.currentUser()!.objectId!) 
    query.findObjectsInBackgroundWithBlock { 
     (objects : [PFObject]?, error: NSError?) -> Void in 

     if error == nil { 
      for object in objects! { 
      let messages = object["file"] as! PFFile 
       self.tableView.reloadData() 
     print("Retreived \(self.messages.count) messages") 
    } 
    } 
} 


} 

崩潰斷點是在查詢的開始。 謝謝社區,另一組經驗豐富的眼睛總是有幫助。

回答

0

您可以使用,如果讓構建

if let objects = objects { 
    for object in objects 
     let messages = object["file"] as? PFFile 
     print(messages) 
     // or you can also safely unwarp here 
     // if let messages = object["file"] as? PFFile { 
     // print(message) 
     // } 
     self.tableView.reloadData() 
     print("Retreived \(self.messages.count) messages") 
} 
} 
+0

感謝您爲它工作的快速反饋安全解開的對象。 – gilbert105

+0

如果這個工作適合你,請標記接受的答案。 – Sahil