How to check if imageView is not nil in swift?

*爱你&永不变心* 提交于 2019-12-13 00:06:41

问题


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")
            }
            }}}

回答1:


Try this:

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

    if let image = UIImage(data: imageData) {

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



回答2:


In order to get this working, you have to understand Optional Chaining. As the Apple Documentation says:

Optional chaining is a process for querying and calling properties, methods, and subscripts on an optional that might currently be nil. If the optional contains a value, the property, method, or subscript call succeeds; if the optional is nil, the property, method, or subscript call returns nil. Multiple queries can be chained together, and the entire chain fails gracefully if any link in the chain is nil.

So if you want an object to get a nil Value, you have to declare it as Optional. To declare the object as Optional You have to place a question mark after the value. In your example it will look just like this:

 var image:UIImage? ;
 image = UIImage(data: imageData) ;

And by declaring this UIImage as Optional, it will be initialized with nil.




回答3:


let image : UIImage? = img

   if image != nil{

   }else{

   }



回答4:


swift 5

var imageBottom = UIImage.init(named: "contactUs")
               if imageBottom != nil {
                   imageBottom = imageBottom?.withRenderingMode(.alwaysTemplate)
                bottomImageview.image = imageBottom
               }



回答5:


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")

    }


来源:https://stackoverflow.com/questions/25521886/how-to-check-if-imageview-is-not-nil-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!