Skip to content

Instantly share code, notes, and snippets.

@mikeschinkel
Last active May 8, 2019 17:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikeschinkel/2156a0aae462179785bb65e6c090a4e9 to your computer and use it in GitHub Desktop.
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
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/
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