我正在通过iOS Apple Developer Tutorial工作,但由于我的计算机版本的Swift是1.2而Xcode是6.4而不是Swift 3.0,因此我遇到了一些问题。我已经能够根据需要同时降级代码,但是在#34; 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
但是我收到了以下错误:"使用未解决的已识别的' 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"
}
}
答案 0 :(得分:0)
这是因为您的变量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)")
}
我希望这会有所帮助
答案 1 :(得分:0)
int i = sex ? 1 : 0;
可选绑定语句创建一个仅存在于以下大括号内的新变量:
if let
guard let创建一个新的变量,该变量在当前作用域结束后从代码中存在:
if let foo = optionalFoo {
//foo is only defined here
}
//foo has gone out of scope.
由于Swift 1没有保护声明,您需要将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)
}
之后的代码移到guard 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。