Swift进阶:traits/mixins/协议扩展的使用场景总

2020-12-04  本文已影响0人  SoaringHeart

实现步骤: 声明协议 - 扩展协议(实现默认方法)- 协议添加给类型或具体功能类或者结构体( - 在类中 - (重写可选)- 使用)
//使用场景 1:给类型扩展方法(你认为合适的任意类型)
extension *: ValidateProtocol{}
//使用场景 2: 给具体功能类/结构体扩展方法(协议方法有默认实现,重写可选)
class *, Validator {
//可重写方法
}

//
//  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 ?? false
    }
    
    ///校验身份证号码
    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: value)
    }
    
    ///校验简单手机号码
    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 {
        if value.count == 0 {
            return false
        }
        let regex = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"
        let predicate = NSPredicate(format: "SELF MATCHES %@", regex)
        return predicate.evaluate(with: value)
    }
}

使用🌰🌰:

//
//  Created by Bin Shang on 2020/12/4.
//  Copyright © 2020 BN. All rights reserved.
//

import UIKit
import SwiftExpand

class ValidateProtocolController: UIViewController {

    ///重写
    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)")
    }

}

//使用场景 1:给类型扩展方法(UIViewController 可以用你认为合适的任意类型代替)
extension UIViewController: ValidateProtocol{}

//使用场景 2:给具体功能类/结构体扩展方法(协议方法有默认实现,重写可选)
class ValidateProtocolController: UIViewController, Validator {
     //想怎么用怎么用
}

2021-01-18 17:37:47.939 ValidateProtocolController.swift.viewWillAppear(_:)[line 29]: false

上一篇下一篇

猜你喜欢

热点阅读