“unexpectedly found nil while unwrapping an Option

2019-02-26 06:58发布

问题:

i've a problem when i retrieve a PFFile stored in Parse.com

let user = PFUser.currentUser()
let userImageFile = user["profileImage"] as PFFile
            userImageFile.getDataInBackgroundWithBlock {
                (imageData: NSData!, error: NSError!) -> Void in
                if error == nil {
                    if imageData != nil{
                        let image = UIImage(data:imageData)
                        self.profileImage.image = image
                    }
                }
            }

The error start at line two. Column "profileImage" exists, but it can be empty.

Someone can help me? Thank you.

回答1:

Try:

if let userImageFile = user["profileImage"] as? PFFile {

}