Created
June 13, 2017 21:05
-
-
Save xxtesaxx/b87a16fdcbedcef0158441bea3c9f23b to your computer and use it in GitHub Desktop.
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
protocol Emptyable { | |
var isEmpty: Bool { get } | |
} | |
extension Optional where Wrapped: Emptyable { | |
func orWhenNilOrEmpty(_ defaultValue: Wrapped) -> Wrapped { | |
switch(self) { | |
case .none: | |
return defaultValue | |
case .some(let value) where value.isEmpty: | |
return defaultValue | |
case .some(let value): | |
return value | |
} | |
} | |
} | |
extension String: Emptyable {} | |
extension Array: Emptyable {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment