Implementation steps: Declare protocol – Extend protocol (implement default method) – Add protocol to type or specific function class or structure (- in class – (override optional) – use)
Use Scenario 1: Extend methods to types (any type you see fit)
extension *: ValidateProtocol{}
Usage Scenario 2: Extend methods to specific function classes/structures (protocol methods have default implementations, override optional)
Class *, Validator {// rewritable method}
// // validateprotocol. swift // SwiftTemplet // // Created by Bin Shang on 2020/12/4. // Copyright © 2020 Bn. All rights reserved. // import UIKit public protocol ValidateProtocol{ func isValidUrl(_ value: String) -> Bool func isValidHttpUrl(_ value: String) -> Bool func isValidFileUrl(_ value: String) -> Bool func isValidIDCard(_ value: String) -> Bool func isValidPhone(_ value: String) -> Bool func isValidEmail(_ value: String) -> Bool } extension ValidateProtocol{ public func isValidUrl(_ value: String) -> Bool { return URL(string: value) ! = nil } public func isValidHttpUrl(_ value: String) -> Bool { guard let url = URL(string: value) else { return false } return url.scheme == "http" } public func isValidFileUrl(_ value: String) -> Bool { return URL(string: value)? .isFileURL ?? Public func isValidIDCard(_ value: String) -> Bool { let regex = "^(^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$)|(^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0 -1])((\\d{4})|\\d{3}[Xx])$)$"; let predicate = NSPredicate(format: "SELF MATCHES %@", regex) return predicate.evaluate(with: Public func isValidPhone(_ value: String) -> Bool { let pattern = "^1[0-9]{10}$" let predicate = NSPredicate(format: "SELF MATCHES %@", pattern) return predicate. Evaluate (with: value)} public func isValidEmail(_ value: String) - > Bool {the if value. Count = = 0} {return false let regex = "[A - Z0-9 a-z. + - _ %] + @ [A - Za - Z0-9. -] + \ \ [A Za - z] {2, 4}" let predicate = NSPredicate(format: "SELF MATCHES %@", regex) return predicate.evaluate(with: value) } }Copy the code
Using 🌰 🌰 :
// // Created by Bin Shang on 2020/12/4. // Copyright © 2020 bn. All rights reserved. // import UIKit import SwiftExpand Class ValidateProtocolController: UIViewController {/ / / rewrite func isValidPhone (_ value: String) -> Bool { return false } override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. } override func viewWillAppear(_ animated: Bool) {super.viewwillAppear (animated) let a = isValidPhone("11111111111") DDLog("\(a)")}} Extension UIViewController (UIViewController can be replaced with any type you see fit) ValidateProtocol {} / / using scenario 2: for the specific function class/structure extension methods (protocol method is the default implementation, rewrite the optional) class ValidateProtocolController: UIViewController, How to use the Validator {/ / want to use} 2021-01-18 17:37:47. 939 ValidateProtocolController. Swift. ViewWillAppear (_) [29] line: falseCopy the code