Skip to content

Instantly share code, notes, and snippets.

@zakbarlow1995
Created March 24, 2020 15:14
Show Gist options
  • Save zakbarlow1995/885fc55edccc443ccec6a2fc54ec47a5 to your computer and use it in GitHub Desktop.
Save zakbarlow1995/885fc55edccc443ccec6a2fc54ec47a5 to your computer and use it in GitHub Desktop.
Swift Collections - add isEmptyOrNil/isNilOrEmpty: Bool & nilIfEmpty: Wrapped? properties
import Foundation
extension Optional where Wrapped: Collection {
var isEmptyOrNil: Bool {
self?.isEmpty ?? true
}
var isNilOrEmpty: Bool {
isEmptyOrNil
}
var nilIfEmpty: Wrapped? {
isEmptyOrNil ? nil : self
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment