本文介绍了用于条件绑定的swift 2初始化程序必须具有Optional类型,而不是'UIImage'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新我的xcode以运行swift 2后,它给了我我难以解决的这两个错误.

After updating my xcode to run swift 2 it gives me these two errors which i struggle to solve.

代码

let image : UIImage = editingInfo[UIImagePickerControllerOriginalImage] as! UIImage

代码

if let constImage = image  (Error2 display here) 
        {
            let targetWidth = UIScreen.mainScreen().scale * UIScreen.mainScreen().bounds.size.width
            let resizedImage = constImage.resize(targetWidth)

            picker.dismissViewControllerAnimated(true, completion: {
                () -> Void in

                NetworkManager.sharedInstance.postImage(resizedImage, completionHandler: {
                    (error) -> () in

                    if let constError = error
                    {
                        self.showAlert(constError.localizedDescription)
                    }
                })

            })
        }

推荐答案

以下代码...

let image : UIImage = editingInfo[UIImagePickerControllerOriginalImage] as! UIImage

如果没有UIImagePickerControllerOriginalImage键或它不是图像,则

...将崩溃.

... is going to crash if there's no UIImagePickerControllerOriginalImage key or if it's not an image.

您是从哪里获得editingInfo的?因为imagePickerController:didFinishPickingImage:editingInfo:在Swift中不可用.您应该使用optional func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]).

From where you did get editingInfo? Because imagePickerController:didFinishPickingImage:editingInfo: is not available in Swift. You should use optional func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]).

您在第二行中遇到的第二个错误...

Your second error on the following line ...

if let constImage = image

...是由let image: UIImage = ...行引起的.您的imageUIImage类型,而不是UIImage?.因此,它不是可选的,您不能在if let constImage = image中使用它.如果要以这种方式使用它,则必须为UIImage?.顺便说一句,不需要使用let image: UIImage = ...let image = ...就足够了,因为编译器可以从您的语句中推断变量类型.

... is caused by let image: UIImage = ... line. Your image is of UIImage type, not UIImage?. Thus it's not optional and you can't use it in if let constImage = image. Must be UIImage? if you want to use it in this way. BTW there's not need to use let image: UIImage = ..., let image = ... is enough, because compiler can infer variable type from your statement.

将其重写为类似的内容.

Rewrite it to something like this.

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {

  guard let image = info[UIImagePickerControllerOriginalImage] as? UIImage else {
    // throw an error, return from your function, whatever
    return
  }
  // from now you can use `image` safely
  // it does exist and it's not optional

  let targetWidth = UIScreen.mainScreen().scale * UIScreen.mainScreen().bounds.size.width
  let resizedImage = image.resize(targetWidth)

  picker.dismissViewControllerAnimated(true, completion: {
    () -> Void in

    NetworkManager.sharedInstance.postImage(resizedImage, completionHandler: {
      (error) -> () in

      if let constError = error
      {
        self.showAlert(constError.localizedDescription)
      }
    })

  })

}

以下部分...

  guard let image = info[UIImagePickerControllerOriginalImage] as? UIImage else {
    // throw an error, return from your function, whatever
    return
  }

...执行此操作...

... does this ...

  • info词典中的UIImagePickerControllerOriginalImage键是否有值?如果否,则执行else {}语句
  • 值,我可以将其转换为UIImage吗?如果否,则执行else {}语句
  • 现在我们已经将info中的值成功转换为UIImage并存储在image中,else {}语句未执行,我们的函数继续.
  • is there a value in info dictionary for UIImagePickerControllerOriginalImage key? if no, else {} statement is executed,
  • value is there, can I cast it to UIImage? if no, else {} statement is executed,
  • now we have value from info successfully casted to UIImage and stored in image, else {} statement is not executed and our function continues.

例如当字典值类型为AnyObject时如何从某种类型的字典中获取值的安全方法.

Safe way how to get a value from dictionary of some type when dictionary value type is AnyObject for example.

这篇关于用于条件绑定的swift 2初始化程序必须具有Optional类型,而不是'UIImage'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 17:13