[英]guard let foo = foo VS guard foo != nil in Swift
[英]If Let vs Guard Let for UIImage in Swift 1.2
我正在通過iOS Apple Developer Tutorial進行工作,但是由於我的計算機的Swift版本是1.2,而Xcode是6.4,而不是Swift 3.0,因此我遇到了一個問題。 我已經能夠根據需要對代碼進行降級處理,但是遇到了以下“ Swift 2.0”中引入的“ guard let”錯誤:
// 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
但出現以下錯誤:“使用未解決的已識別'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"
}
}
發生這種情況是因為您只有在if let
范圍內定義了selectedImage
變量
您必須改用此代碼
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。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.