Last active
November 29, 2016 09:56
-
-
Save evgeniyd/c77b6f59dc2a6624fe7a557c26e1e6b0 to your computer and use it in GitHub Desktop.
contains(_:) variant to work with Optional<UnicodeScalar>
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
// Swift 3 | |
extension CharacterSet { | |
/// Test for membership of a particular `UnicodeScalar?` in the `CharacterSet`. | |
/// - important: This `contains(_:)` oveload works with `Optional<UnicodeScalar>` | |
/// | |
/// Consider the following example: | |
/// ```` | |
/// let flowermoji = "💐" | |
/// let ucscalar = UnicodeScalar(flowermoji.utf16.last!) | |
/// // ucscalar == nil | |
/// | |
/// if CharacterSet.whitespaces.contains(ucscalar) { | |
/// print("contatins") | |
/// } | |
/// else { | |
/// print("does not contain") | |
/// } | |
/// // does not contain | |
/// ```` | |
/// - returns: `false` when `member==.none`, otherwise it works as an original `contains(_: UnicodeScalar)` | |
/// - parameter member: optional unicode scalar which membership should be tested | |
public func contains(_ member: UnicodeScalar?) -> Bool { | |
guard let unicodeScalar: UnicodeScalar = member else { | |
return false | |
} | |
return self.contains(unicodeScalar) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment