Skip to content

Instantly share code, notes, and snippets.

@edopelawi
Created September 13, 2016 15:14
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 edopelawi/990735339cdef47f78b391c0455445e3 to your computer and use it in GitHub Desktop.
Save edopelawi/990735339cdef47f78b391c0455445e3 to your computer and use it in GitHub Desktop.
A sample on getting a default value for Optional with nil cases using if let and guard let statement.
func getValidString(string: String?) -> String {
if let validString = string {
return validString
} else {
return ""
}
}
func anotherGetValidString(string: String?) -> String {
guard let validString = string else {
return ""
}
return validString
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment