How to check if imageView is not nil in swift?

jmcastel

I have a fatal error while unwrapping an optional value in swift.

I have a profile ViewController with a background image and an avatar image witch are the same.

When the user has not an image set, i ve got this fatal error, instead i would like to add a "by default image Shape".

How could i check if image isn't nil ?

This is my code :

 var currentUser = PFUser.currentUser()

    let User = currentUser as PFUser
    let  userImage:PFFile = User["profileImage"] as PFFile {

 userImage.getDataInBackgroundWithBlock{(imageData:NSData!, error:NSError!)-> Void in

        if !(error != nil) {

            var image:UIImage! = UIImage(data: imageData)

            if image != 0 {
                self.backgroundImageUser.image = image
                self.avatarUserView.image = image
            }
            else if image == 0 {
                self.backgroundImageUser.image = UIImage(named: "Shape")
                self.avatarUserView.image = UIImage(named: "Shape")
            }
            }}}
jmcastel

In fact the problem was up, as you can see in the edited post, i had a the userImage declaration not optional.

So now everything work fine with :

var currentUser = PFUser.currentUser()

    let User = currentUser as PFUser
    if let  userImage:PFFile = User["profileImage"] as? PFFile {

    userImage.getDataInBackgroundWithBlock{(imageData:NSData!, error:NSError!)-> Void in

        if !(error != nil) {

            var image :UIImage! = UIImage(data: imageData)

                self.backgroundImageUser.image = image
                self.avatarUserView.image = image

        }

        }}

     else {
        self.backgroundImageUser.image = UIImage(named: "Shape")
        self.avatarUserView.image = UIImage(named: "Shape")

    }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related