Created
March 24, 2020 15:14
-
-
Save zakbarlow1995/885fc55edccc443ccec6a2fc54ec47a5 to your computer and use it in GitHub Desktop.
Swift Collections - add isEmptyOrNil/isNilOrEmpty: Bool & nilIfEmpty: Wrapped? properties
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
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