Created
February 13, 2020 19:00
-
-
Save lourintp/bb2ee21cd7d899140e4012ec30b5502c to your computer and use it in GitHub Desktop.
Example of a UITextField validator that can be replaced for a Strategy Pattern
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
| import UIKit | |
| import Foundation | |
| struct TextFieldValidator { | |
| enum FieldValidationType { | |
| case empty | |
| case sizeGreaterThenThree | |
| case onlyNumbers | |
| } | |
| let fieldValidationType: FieldValidationType | |
| func validate(field: UITextField) -> Bool { | |
| guard let text = field.text else { return false } | |
| switch fieldValidationType { | |
| case .empty: | |
| return text.isEmpty | |
| case .onlyNumbers: | |
| return CharacterSet.decimalDigits.isSuperset(of: CharacterSet(charactersIn: text)) | |
| case .sizeGreaterThenThree: | |
| return text.count > 3 | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment