Skip to content

Instantly share code, notes, and snippets.

@joshdholtz
Last active January 14, 2016 15:27
Show Gist options
  • Save joshdholtz/5b37db6d55068b2dd4df to your computer and use it in GitHub Desktop.
Save joshdholtz/5b37db6d55068b2dd4df to your computer and use it in GitHub Desktop.
Swift - Add isEmpty to Optional<String>
extension Optional where Wrapped: StringLiteralConvertible {
var isEmpty: Bool {
return true
}
}
let maybeString: String? = "Hello"
// Previously
let isMaybeStringEmpty = maybeString?.isEmpty ?? true
let isMaybeStringEmpty = (maybeString ?? "").isEmpty
// Now
let isMaybeStringEmpty = maybeString?.isEmpty
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment