Skip to content

Instantly share code, notes, and snippets.

@xxtesaxx
Created June 13, 2017 21:05
Show Gist options
  • Save xxtesaxx/b87a16fdcbedcef0158441bea3c9f23b to your computer and use it in GitHub Desktop.
Save xxtesaxx/b87a16fdcbedcef0158441bea3c9f23b to your computer and use it in GitHub Desktop.
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