Last active
August 29, 2015 14:28
-
-
Save danielctull/1396bb194a9b6ac9943f to your computer and use it in GitHub Desktop.
Using Optional.Some() to condense two guard statements into one.
This file contains hidden or 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
| var phone: String? { | |
| guard | |
| let phone = phoneNumberTextField?.text, | |
| let phoneDetector = try? NSDataDetector(types: NSTextCheckingType.PhoneNumber.rawValue), | |
| let range = pure(NSRange(location: 0, length: phone.startIndex.distanceTo(phone.endIndex))), | |
| let matches = pure(phoneDetector.matchesInString(phone, options: [], range: range)) | |
| where matches.count > 0 | |
| else { | |
| return nil | |
| } | |
| return phone | |
| } | |
| func pure<T>(a: T) -> T? { | |
| return .Some(a) | |
| } |
This file contains hidden or 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
| var phone: String? { | |
| guard | |
| let phone = phoneNumberTextField?.text, | |
| let phoneDetector = try? NSDataDetector(types: NSTextCheckingType.PhoneNumber.rawValue), | |
| let range = Optional.Some(NSRange(location: 0, length: phone.startIndex.distanceTo(phone.endIndex))), | |
| let matches = Optional.Some(phoneDetector.matchesInString(phone, options: [], range: range)) | |
| where matches.count > 0 | |
| else { | |
| return nil | |
| } | |
| return phone | |
| } |
This file contains hidden or 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
| var phone: String? { | |
| guard | |
| let phone = phoneNumberTextField?.text, | |
| let phoneDetector = try? NSDataDetector(types: NSTextCheckingType.PhoneNumber.rawValue) | |
| else { | |
| return nil | |
| } | |
| let range = NSRange(location: 0, length: phone.startIndex.distanceTo(phone.endIndex)) | |
| let matches = phoneDetector.matchesInString(phone, options: [], range: range) | |
| if matches.count == 0 { | |
| return nil | |
| } | |
| return phone | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment