I am trying this Apple tutorial but I found an error. Can someone help me?
Disable Saving When the User Doesn't Enter an Item Name
What happens if a user tries to save a meal with no name? Because the meal property on MealViewController is an optional and you set your initializer up to fail if there’s no name, the Meal object doesn’t get created and added to the meal list—which is what you expect to happen. But you can take this a step further and keep users from accidentally trying to add meals without a name by disabling the Save button while they’re typing a meal name, and checking that they’ve specified a valid name before letting them dismiss the keyboard.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
super.prepare(for: segue, sender: sender)
// Configure the destination view controller only when the save button is pressed.
guard let button = sender as? UIBarButtonItem, button === saveButton else {
if #available(iOS 10.0, *) {
os_log("The save button was not pressed, cancelling", log: OSLog.default, type: .debug)
} else {
// Fallback on earlier versions.
}
return
}
let name = nameTextField.text ?? ""
let photo = photoImageView.image
let rating = ratingControl.rating
// Set the meal to be passed to MealTableViewController after the unwind segue.
meal = Meal(name: name, photo: photo, rating: rating)
}
You probably have a problem with the saveButton
property declaration. It should be typed as UIBarButtonItem
(and not (UIBarButtonItem) -> Void
as your error shows).
To be safe, try repeating the To connect the Save button to the MealViewController code step from the Apple tutorial.