I am working my way through the iOS Apple Developer Tutorial, but am having a bit of an issue since my computer's version of Swift is 1.2 and Xcode is 6.4 instead of Swift 3.0. I've been able to work along-side downgrading the code as need, but have hit an error in "guard let", which was introduced in Swift 2.0, below:
// 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)")
}
I converted it to the following:
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
but I got the following error: "Use of unresolved identified 'selectedImage'"
Any help would be greatly appreciated!
EDIT: FULL CODE BELOW
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"
}
}