來自'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 联系我们: