来自'UIImage'的沮丧? 'UIImage'只展开选项

[英]Downcast from 'UIImage?' to 'UIImage' only unwraps optionals


I'm creating a UIButton with an image,

我正在用图像创建一个UIButton,

I have written the below code for that:

我为此编写了以下代码:

    let btnImg=UIButton.buttonWithType(UIButtonType.Custom) as UIButton
    let img  = UIImage(named: "turn_left") as UIImage
    btnImg.setTitle("Turn left", forState: UIControlState.Normal)
    btnImg.setImage(img, forState: UIControlState.Normal)
    btnImg.frame = CGRectMake(10, 150, 200, 45)
    self.view.addSubview(btnImg)

But I got the error below at let img = UIImage(named: "turn_left") as UIImage:

但是我在img = UIImage(名为:“turn_left”)的UIImage中得到了以下错误:

Swift Compiler error:
 Downcast from 'UIImage?' to 'UIImage' only unwraps optionals; did you mean to use '!'?

4 个解决方案

#1


36  

As error Says You have to use '!' ,

正如错误所述你必须使用'!' ,

Try Below code,

试试下面的代码,

   let img  = UIImage(named: "turn_left") as UIImage!  // implicitly unwrapped

OR

要么

   let img : UIImage? = UIImage(named: "turn_left")  //optional

Edit

编辑

After creating img you need to check it for nil before using it.

创建img后,您需要在使用之前将其检查为nil。

#2


15  

You can always do it in an 'if let' but note the difference in syntax depending on the version of swift.

您总是可以在'if let'中执行此操作,但请注意语法的差异,具体取决于swift的版本。

Swift < 2.0:

斯威夫特<2.0:

if let img = img as? UIImage {

Swift 2.0:

Swift 2.0:

if let img = img as UIImage! {

Note the position of the exclamation

注意感叹号的位置

#3


2  

If the UIImage initialiser cannot find the file specified (or some other error happened), it will return nil, as per Apple's documentation

如果UIImage初始化程序找不到指定的文件(或发生其他一些错误),它将返回nil,根据Apple的文档

Return Value
The image object for the specified file, or nil if the method could not find the specified image.

So you need to put checks in:

所以你需要把支票放入:

let img  = UIImage(named: "turn_left")
if(img != nil) {
    // Do some stuff with it
}

You don't need to cast a UIImage to a UIImage, that's a waste.

你不需要将UIImage转换为UIImage,这是一种浪费。

Edit: Full code

编辑:完整代码

let img  = UIImage(named: "turn_left")
if(img != nil) {
    let btnImg = UIButton.buttonWithType(UIButtonType.Custom) as UIButton
    btnImg.setTitle("Turn left", forState: UIControlState.Normal)
    btnImg.setImage(img, forState: UIControlState.Normal)
    btnImg.frame = CGRectMake(10, 150, 200, 45)
    self.view.addSubview(btnImg)
}

You may want to put an else in to set the button to a shape or something else that is more likely to work in the case "turn_left" doesn't exist.

您可能希望将其他用于将按钮设置为某个形状或其他更有可能在“turn_left”不存在的情况下起作用的内容。

#4


2  

Since UIImage(named:"something") returns an optional (because it could be nil if the methods doesn't find an appropriate image the best this is not to explicitly unwrap the result (otherwise your app will crash) but to check for the result immediately with something like that:

由于UIImage(名为:“something”)返回一个可选项(因为如果方法找不到合适的图像,它可能是nil,最好不要显式解包结果(否则你的应用程序会崩溃)但要检查立即产生这样的结果:

        if let image = UIImage(named: "something"){
        // now you can use image without ? because you are sure to have an image here!
        image.description
        }

    // continue your code here using the optional power of swift's vars! :)

The approach is the following: If the image is optional means that it can be null. Functions that takes in input an optional can handle that, otherwise they usually have unexpected behavior. The UIImage(named:) can return nil and you HAVE to handle this. If you explicitly unwrap it you could have problems later.

方法如下:如果图像是可选的,则意味着它可以为null。输入可选输入的函数可以处理,否则它们通常具有意外行为。 UIImage(名为:)可以返回nil,你必须处理这个问题。如果您明确打开它,以后可能会遇到问题。

With if let something = ... something will be automatically unwrapped in runtime and you can use it safely.

如果让某些东西= ......某些内容将在运行时自动解包,您可以安全地使用它。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2014/11/13/5be260b7df179fd17f2539130963569c.html



 
  © 2014-2022 ITdaan.com 联系我们: