Last active
May 8, 2019 17:20
-
-
Save mikeschinkel/2156a0aae462179785bb65e6c090a4e9 to your computer and use it in GitHub Desktop.
Proof-of-concept example of Swift using The Break/Continue Guard Clause pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Here is an example of using what I am currently calling the Break/Continue Guard Clause pattern. | |
See: | |
1. https://mikeschinkel.me/2019/better-alternative-to-return-early-php-wordpress/ | |
2. https://thatthinginswift.com/guard-statement-swift/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let Once = [1] | |
func submit() { | |
var ok = false | |
for only in Once { | |
guard let name = nameField.text else { | |
show("No name to submit") | |
break | |
} | |
guard let address = addressField.text else { | |
show("No address to submit") | |
break | |
} | |
guard let phone = phoneField.text else { | |
show("No phone to submit") | |
break | |
} | |
sendToServer(name, address: address, phone: phone) | |
ok = true | |
} | |
return ok | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment