iOS - Retrieve and display an image from Parse in UIImageView (Swift 1.2 Error)

前端 未结 1 1242
迷失自我
迷失自我 2020-12-21 05:58

I have previously been retrieving an image form my Parse backend to display in my app inside a UIImageView using the following lines of code:

let userPicture         


        
相关标签:
1条回答
  • 2020-12-21 06:30

    PFUser.currentUser() returns optional type (Self?). So you should unwrap return value to access element by subscript.

    PFUser.currentUser()?["picture"]
    

    And the value got by subscript is also optional type. So you should use optional binding to cast the value because type casting may fail.

    if let userPicture = PFUser.currentUser()?["picture"] as? PFFile {
    

    And parameters of the result block of getDataInBackgroundWithBlock() method are both optional type (NSData? and NSError?). So you should specify optional type for the parameters, instead NSData and NSError.

    userPicture.getDataInBackgroundWithBlock { (imageData: NSData?, error: NSError?) -> Void in
    

    The code modified the all above problems is below:

    if let userPicture = PFUser.currentUser()?["picture"] as? PFFile {
        userPicture.getDataInBackgroundWithBlock { (imageData: NSData?, error: NSError?) -> Void in
            if (error == nil) {
                self.dpImage.image = UIImage(data:imageData)
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题