Last active
February 21, 2020 14:48
-
-
Save lourintp/20c334518d2860ab30756696c8b45576 to your computer and use it in GitHub Desktop.
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
| protocol TextFieldValidatorStrategy { | |
| func validate(_ field: UITextField) -> Bool | |
| } | |
| struct TextFieldValidator { | |
| let strategy: TextFieldValidatorStrategy | |
| func validate(_ field: UITextField) -> Bool { | |
| if let _ = field.text { | |
| return strategy.validate(field) | |
| } | |
| return false | |
| } | |
| } | |
| struct EmptyTextFieldValidatorStrategy: TextFieldValidatorStrategy { | |
| func validate(_ field: UITextField) -> Bool { | |
| return field.text!.isEmpty | |
| } | |
| } | |
| struct GreaterThenThreeTextFieldValidatorStrategy: TextFieldValidatorStrategy { | |
| func validate(_ field: UITextField) -> Bool { | |
| return field.text!.count > 3 | |
| } | |
| } | |
| struct OnlyNumbersTextFieldValidatorStrategy: TextFieldValidatorStrategy { | |
| func validate(_ field: UITextField) -> Bool { | |
| return CharacterSet.decimalDigits.isSuperset(of: CharacterSet(charactersIn: field.text!)) | |
| } | |
| } | |
| TextFieldValidator(strategy: EmptyTextFieldValidatorStrategy()).validate(myTextField) | |
| TextFieldValidator(strategy: GreaterThenThreeTextFieldValidatorStrategy()).validate(myTextField) | |
| TextFieldValidator(strategy: OnlyNumbersTextFieldValidatorStrategy()).validate(myTextField) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment