Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在swift中检查imageView是否为非零?_Ios_Swift_Optional Variables - Fatal编程技术网

Ios 如何在swift中检查imageView是否为非零?

Ios 如何在swift中检查imageView是否为非零?,ios,swift,optional-variables,Ios,Swift,Optional Variables,在swift中展开可选值时出现致命错误 我有一个配置文件ViewController,背景图像和化身图像是相同的 当用户没有图像集时,我遇到了这个致命错误,相反,我想添加一个“默认图像形状” 如何检查图像是否为零 这是我的代码: var currentUser = PFUser.currentUser() let User = currentUser as PFUser let userImage:PFFile = User["profileImage"] as PFFil

在swift中展开可选值时出现致命错误

我有一个配置文件ViewController,背景图像和化身图像是相同的

当用户没有图像集时,我遇到了这个致命错误,相反,我想添加一个“默认图像形状”

如何检查图像是否为零

这是我的代码:

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

为了让这个工作,你必须了解可选链接。 正如上面所说:

可选链接是查询和调用当前可能为零的可选链接上的属性、方法和下标的过程。如果可选项包含值,则属性、方法或下标调用成功;如果可选值为nil,则属性、方法或下标调用将返回nil。多个查询可以链接在一起,如果链中的任何链接为零,则整个链都会正常失败

因此,如果希望对象获得nil值,则必须将其声明为可选。要将对象声明为可选对象,必须在值后面加一个问号。 在您的示例中,它将如下所示:

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

通过将此UIImage声明为可选,它将被初始化为nil。

为了使其正常工作,您必须了解可选链接。 正如上面所说:

可选链接是查询和调用当前可能为零的可选链接上的属性、方法和下标的过程。如果可选项包含值,则属性、方法或下标调用成功;如果可选值为nil,则属性、方法或下标调用将返回nil。多个查询可以链接在一起,如果链中的任何链接为零,则整个链都会正常失败

因此,如果希望对象获得nil值,则必须将其声明为可选。要将对象声明为可选对象,必须在值后面加一个问号。 在您的示例中,它将如下所示:

 var image:UIImage? ;
 image = UIImage(data: imageData) ;
通过将此UIImage声明为可选,它将被初始化为nil。

尝试以下操作:

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")
    }
}
试试这个:

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

事实上,问题已经出现了,正如你在编辑的帖子中看到的,我有一个用户图像声明不是可选的

因此,现在一切都很好:

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

    }

事实上,问题已经出现了,正如你在编辑的帖子中看到的,我有一个用户图像声明不是可选的

因此,现在一切都很好:

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

    }
斯威夫特5

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

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

您可以在else之后删除第二个if。如果<代码>图像!=0第二个if是不需要的。你必须知道,即使没有真正的错误,
错误也可能是一个有效的对象(例如,code:0,OK),因此像这样检查
错误是不安全的–逻辑可以在Objc中工作,但在Swift中不是那么简单,只要
错误
不是可选的。您是如何声明用户映像的?您可以在else之后删除第二个if。如果<代码>图像!=0
第二个if是不需要的。你必须知道,即使没有真正的错误,
错误也可能是一个有效的对象(例如,code:0,OK),因此像这样检查
错误是不安全的–逻辑可以在Objc中工作,但在Swift中不是那么简单,只要
错误
不是可选的。您是如何声明userImage的?他已经将其声明为默认的未包装可选(
var-image:UIImage!=…
),这是相同的。这是错误的。仅当您要展开可选值时,才使用“!”。要将其声明为可选项,必须使用“?”。。如果你像以前那样申报的话。如果您已准备好展开该值,则该值不能为零。检查变量是否为nil,而它仍然为wrappedNo<代码>不仅仅用于表达式中。A
是隐式WrappedOptional,这是可选类型的类型。他已将其声明为隐式展开的可选类型(
var image:UIImage!=…
),这与此相同。这是错误的。仅当您要展开可选值时,才使用“!”。要将其声明为可选项,必须使用“?”。。如果你像以前那样申报的话。如果您已准备好展开该值,则该值不能为零。检查变量是否为nil,而它仍然为wrappedNo<代码>不仅仅用于表达式中。A
是隐式WrappedOptional,它是可选类型的类型。