There is such a code. And for some reason, the guard instruction does not work.
struct User { var firstName: String var lastName: String var age: String } let firstNameTextField = UITextField() let lastNameTextField = UITextField() let ageTextField = UITextField() firstNameTextField.text = "Александр" lastNameTextField.text = "Иванов" ageTextField.text = nil func createUser() -> User? { guard let firstNameUnwrap = firstNameTextField.text else { return nil } guard let lastNameUnwrap = lastNameTextField.text else { return nil } guard let ageUnwrap = ageTextField.text else { return nil } return User(firstName: firstNameUnwrap, lastName: lastNameUnwrap, age: ageUnwrap) } if let userNew = createUser() { print(userNew.firstName, userNew.lastName, userNew.age) } Prints - "Alexander Ivanov"
Tell me, please, why the guard does not work? After all, it must interrupt the execution of the code, because ageTextField.text = nil. Those. nothing should be printed.