Last active
January 14, 2016 15:27
-
-
Save joshdholtz/5b37db6d55068b2dd4df to your computer and use it in GitHub Desktop.
Swift - Add isEmpty to Optional<String>
This file contains hidden or 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
extension Optional where Wrapped: StringLiteralConvertible { | |
var isEmpty: Bool { | |
return true | |
} | |
} |
This file contains hidden or 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 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