guard:
@IBAction func saveButtonTapped(_ sender: Any) {
guard let title = titleTextField.text,
title.count > 0,
let author = authorTextField.text,
author.count > 0 else {
return
}
book = Book(title: title, author: author)
performSegue(withIdentifier: PropertyKeys.unwind, sender: self)
}
if let:
@IBAction func saveButtonTapped(_ sender: Any) {
if let name = nameTextField.text,
let employeeType = employeeType {
employee = Employee(name: name, dateOfBirth: dobDatePicker.date, employeeType: employeeType)
performSegue(withIdentifier: PropertyKeys.unwindToListIndentifier, sender: self)
}
}
利用 ?? (nil-coalescing operator)设定预设值:
var registration: Registration {
let firstName = firstNameTextField.text ?? ""
let lastName = lastNameTextField.text ?? ""
return Registration(firstName: firstName,
lastName: lastName)
}