Skip to content

Instantly share code, notes, and snippets.

@shaps80
Last active May 4, 2018 09:26
Show Gist options
  • Select an option

  • Save shaps80/1cf3153b80ae7a6da02b06ac311fe7b6 to your computer and use it in GitHub Desktop.

Select an option

Save shaps80/1cf3153b80ae7a6da02b06ac311fe7b6 to your computer and use it in GitHub Desktop.
Adds `isNilOrEmpty` to any `Collection` – Swift 4
public extension Optional where Wrapped: Collection {
/// Equivalent to `if let value = value, !value.isEmpty`
///
/// Useful when you need to know whether or not an underlying value exists at all.
///
/// string.isNilOrEmpty
///
/// Returns true if `string` is nil or empty. False otherwise
var isNilOrEmpty: Bool {
switch self {
case .some(let wrapped): return wrapped.isEmpty
case .none: return true
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment