Skip to content

Instantly share code, notes, and snippets.

@lourintp
Created February 13, 2020 19:00
Show Gist options
  • Select an option

  • Save lourintp/bb2ee21cd7d899140e4012ec30b5502c to your computer and use it in GitHub Desktop.

Select an option

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
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