二元运算符'!='不能应用于" UIImage"类型的操作数。

时间:2017-06-15 16:28:01

标签: ios swift uiimage

我尝试使用下面的if语句检查是否有图像:

var profileImage = UIImage()


 @IBAction func submitAction(_ sender: Any) {


    if profileImage != 0 && nameTextField.text != nil {


    let storageRef = FIRStorage.storage().reference()
    let databaseRef = FIRDatabase.database().reference()

    let uid = FIRAuth.auth()?.currentUser?.uid

    let profileImageData = UIImageJPEGRepresentation(self.profileImage, 0.4)

    let profileImageRef = storageRef.child("ProfileImages/\(uid)/profileImage.jpg")
    profileImageRef.put(profileImageData!, metadata: nil) {
        metadata, error in

        if error != nil{

            print("error!")

        }
        else{

            let downloadURL = metadata?.downloadURL()
            let username = self.nameTextField.text
            let user : [NSObject : AnyObject] = ["uid" as NSObject : uid! as AnyObject, "username" as NSObject : username! as AnyObject, "profileImageURL" as NSObject : downloadURL! as AnyObject]



            let childUpdates = ["Users/\(uid)/" : user]
            databaseRef.updateChildValues(childUpdates)

        }
        }
        }

    }

然而它返回错误:

'二元运算符'!='不能应用于" UIImage'

类型的操作数

我也尝试用0替换0,但这不适用于UIImage。

1 个答案:

答案 0 :(得分:2)

将profileImage定义为可选项,然后您可以使用guard语句检查它是否具有值。

var profileImage:UIImage?
guard let profileImage = profileImage, let name = nameTextField.text else { return}
let storageRef = FIRStorage.storage().reference()
let databaseRef = FIRDatabase.database().reference()
...