If Let vs Guard Let for UIImage in Swift 1.2

If Let vs Guard Let for UIImage in Swift 1.2

我正在学习 iOS Apple Developer Tutorial,但遇到了一些问题,因为我的计算机版本 Swift 是 1.2 而 Xcode 是 6.4 Swift 3.0。我已经能够根据需要一起降级代码,但在 "guard let" 中遇到了错误,该错误在 Swift 2.0 中引入,如下:

// The info dictionary may contain multiple representations of the image. 
// You want to use the original.

guard let selectedImage = info[UIImagePickerControllerOriginalImage] as? UIImage else {
    fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
}

我将其转换为以下内容:

if let selectedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
     return selectedImage
} else {
     fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
}

//Set photoImageView to display the image.
photoImageView.image = selectedImage

但我收到以下错误:"Use of unresolved identified 'selectedImage'"

如有任何帮助,我们将不胜感激!

编辑:完整代码如下

import UIKit

class ViewController: UIViewController, UITextFieldDelegate,
UIImagePickerControllerDelegate, UINavigationControllerDelegate {

//MARK: Properties
@IBOutlet weak var nameTextField: UITextField!
@IBOutlet weak var mealNameLabel: UILabel!
@IBOutlet weak var photoImageView: UIImageView!

override func viewDidLoad() {
    super.viewDidLoad()

    //Handle the text field's user input through the delegate callbacks
    nameTextField.delegate = self
}


//MARK: UITextFieldDelegate
func textFieldShouldReturn(textField: UITextField) -> Bool {
    //Hide the keyboard
    textField.resignFirstResponder()
    return true
}
func textFieldDidEndEditing(textField: UITextField) {
    //Set the lablel name as what the used typed
    mealNameLabel.text = textField.text
}

//MARK: UIImagePickerControllerDelegate
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
    //Dismiss the picker if the user canceled
    dismissViewControllerAnimated(true, completion:nil)
}
func imagePickerController(picker: UIImagePickerController,
    didFinishPickingMediaWithInfo info: [String : Any]) {
        //The info dictionary may contain multiple representations of the image. You want to use the original.
        if let selectedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
            //Set photoImageView to display the image
            photoImageView.image = selectedImage
        } else {
            fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
        }

        //Set photoImageView to display the image
        //photoImageView.image = selectedImage

        //Dismiss the picker
        dismissViewControllerAnimated(true, completion: nil)
}


//MARK: Actions
@IBAction func selectImageFromPhotoLibrary(sender: UITapGestureRecognizer) {
    //Hide the keyboard
    nameTextField.resignFirstResponder()

    //UIImagePickerController is a view controller that lets a user pick media from their photo library.
    let imagePickerController = UIImagePickerController()


    //Only allow photos to be picked, not taken.
    imagePickerController.sourceType = .PhotoLibrary

    //Make sure ViewController is notified when the user picks and image.
    imagePickerController.delegate = self
    presentViewController(imagePickerController, animated: true, completion: nil)

}
@IBAction func setDefaultLabelText(_: UIButton) {
    mealNameLabel.text = "Default Text"
}
}

发生这种情况是因为变量 selectedImage 仅在 if let 范围内定义

您必须改用此代码

if let selectedImage = info[UIImagePickerControllerOriginalImage] as? UIImage{
     //Set photoImageView to display the image.
     photoImageView.image = selectedImage
     return selectedImage
} else {
     fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
}

希望对您有所帮助

if let 可选绑定语句创建一个新变量,该变量仅存在于后面的大括号内:

if let foo = optionalFoo {
  //foo is only defined here
}

//foo has gone out of scope.

guard let 创建一个新变量,该变量在当前范围结束后的代码中存在:

func bar(optionalFoo: FooThing) {
  guard let foo = optionalFoo else {return}

  //foo exists from here until the end of the 
  //current scope (the end of the func)
}

由于Swift 1 没有保护语句,您需要将guard let 之后的代码移动到if let 的大括号内:

看起来@ReinierMelian 用他的回答打败了我。在 Swift 1.x.

中查看他的答案以了解正确的代码形式

请注意 Swift 1.0 已经严重过时了,您使用它是在损害自己的利益。您应该升级到 Xcode 8.3 和 Swift 3.1。从 Swift 1 到 Swift 2 的变化是显着的,从 Swift 2 到 Swift 3 的变化是 。 Swift 1 代码无法在 Xcode 8 中编译,您可能无法通过自动转换 运行 它。 (Xcode 8 将通过自动转换为 Swift 3 提供给 运行 Swift 2.x,但我不确定它是否会转换 Swift 1 到 Swift 3.