swift WisdomProtocol SDK 面向協議編程

@[TOC] WisdomProtocol SDK 面向協議編程

# Welcome to use WisdomProtocol

WisdomProtocol is a Protocol of wisdom sdk. 

**github install: pod 'WisdomProtocol'**

 

Function Description:

1). WisdomProtocol sdk: Support compatible syntax 'Swift/objective-c' protocol usage.

2). WisdomProtocol sdk: Support sdk across 'module/project/static library/dynamic library' internal 'Class/UIViewController/UIView/Param' routing protocol.

3). WisdomProtocol sdk: Support data types 'dictionary/json/model/model collection' to 'encoding/decoding' conversion protocol.

   - 'Set class/data' to 'encoding/decoding' task, in the Debug environment, conversion failure, added the assertion processing, it is easy to find data hidden dangers during debugging:

     * assert(able != nil, "decodable failure: \(value)") *

     * assert(dict != nil, "decodable failure: \(able)") *

4). WisdomProtocol sdk: Support object enablement 'objective-c/Swift Class' timer 'forward/countdown' task protocol. 

   - Timer life cycle/release does not require the user's concern, the use of timers is maintained/managed automatically within the WisdomProtocol sdk.

5). WisdomProtocol sdk: Support for capture and tracking 'objective-c/Swift Class' when a run crash error occurs, log trace, capture protocol.

6). WisdomProtocol sdk: Support statistical tracking 'UIViewController' 'viewDidAppear(_:)/viewDidDisappear(_:)' page display time and duration statistics protocol.

 

-中文-

# 歡迎使用WisdomProtocol

WisdomProtocol是一個智能協議 sdk。

**github 集成: pod 'WisdomProtocol'**

 

功能描述:

1). WisdomProtocol sdk: 支持兼容語法 'Swift/objective-c' 協議使用。

2). WisdomProtocol sdk: 支持sdk跨越 '模塊/項目/靜態庫/動態庫' 內部 'Class/UIViewController/UIView/Param' 路由協議。

3). WisdomProtocol sdk: 支持數據類型 '字典/字典數組/json/模型/模型數組' 的 '編碼/解碼' 轉換協議。

  - '集合 類/數據' 的 '編碼/解碼' 任務,在調試環境下,轉換失敗,添加了斷言處理,便於調試階段,發現數據隱患:

    * assert(able != nil, "decodable failure: \(value)") *

    * assert(dict != nil, "decodable failure: \(able)") *

4). WisdomProtocol sdk: 支持對象啓用 'objective-c/Swift Class' 定時器 '前進計時/倒計時' 任務協議。

   - 計時器的 生命週期/釋放時機 不需要用戶關注,計時器的使用在 WisdomProtocol sdk內部,會自動 管理/維護。

5). WisdomProtocol sdk: 支持捕捉跟蹤 'objective-c/Swift Class' 發生運行崩潰錯誤時,日誌跟蹤,捕捉協議。

6). WisdomProtocol sdk: 支持統計跟蹤 'UIViewController' 'viewDidAppear(_:)/viewDidDisappear(_:)' 頁面展示時機和時長 統計協議。

 

##  ----------------------------------------

## 路由協議篇

## 路由協議是 WisdomProtocol 核心功能,以下爲您介紹如何去使用

【**協議支持**】:跨工程/模塊/動態庫/靜態庫 中 類/UIView/UIViewController/參數 之間的交互/傳遞/調用功能。

【**註冊綁定**】:路由協議在使用之前,需要實現 WisdomRegisterable 註冊協議,將唯一的協議與類綁定,註冊到 WisdomProtocol sdk 中,像這樣:

    @objc protocol WisdomProtocolLeftVCProtocol {}

    class WisdomProtocolLeftVC: UIViewController, WisdomRegisterable, WisdomProtocolLeftVCProtocol {

       // 這裏協議與類進行註冊綁定

       static func registerable() -> WisdomClassable {

           return WisdomClassable(register: WisdomProtocolLeftVCProtocol.self, conform: Self.self)

       }

    }

 

【**路由功能**】

 【1】. Class 路由協議:

    // MARK: - Router Class Protocol

    @objc public protocol **WisdomRouterClassable** {

       // MARK: Param - Any?

       @discardableResult

       @objc optional static func routerClassable(param: Any?)->Self

 

       // MARK: Param - Any?, ((Any)->Void)?

       @discardableResult

       @objc optional static func routerClassable(param: Any?, closure: ((Any)->Void)?)->Self

 

       // MARK: Param - Any?, ((Any)->(Any))?

       @discardableResult

       @objc optional static func routerClassable(param: Any?, returnClosure: ((Any)->(Any))?)->Self

    }

 

---> **Class 路由使用案例**:

註冊綁定:

    @objc protocol WisdomProtocolLeftModelable {}

    class WisdomProtocolLeftModel: WisdomRegisterable, WisdomProtocolLeftModelable {

       // 註冊綁定協議/類

       static func registerable() -> WisdomClassable {

          return WisdomClassable(register: WisdomProtocolLeftModelable.self, conform: Self.self)

       }

       var bgColor: String?

       var textColor: String?

       var codeColor: String?

    }

協議實現:

    extension WisdomProtocolLeftModel: WisdomRouterClassable{

        // 實現路由協議

       static func routerClassable(param: Any?) -> Self {

           var dict: [String:Any] = [:]

           if let value = param as? [String:Any] {

               dict = value

           }

           let cla = WisdomProtocolLeftModel.decodable(value: dict)

           return cla as! Self

        }

    }

---> **外部開始路由**:

    // 通過註冊到協議,獲取到類別

    let cla = WisdomProtocol.getRouterClassable(from: WisdomProtocolLeftModelable.self)

    let param = ["bgColor":"708069","textColor":"FFFFFF","codeColor":"33A1C9"]

    // 創建對象

    let objc = cla?.routerClassable?(param: param)

    print(objc)

 

 【2】. UIViewController 路由協議:

    // MARK: - Router UIViewController Protocol

    @objc public protocol WisdomRouterControlable where Self: UIViewController {

       // MARK: Param - UIViewController?, Any?

       @discardableResult

       @objc optional static func routerControlable(rootVC: UIViewController?, param: Any?)->Self

 

       // MARK: Param - UIViewController?, Any?, ((Any)->Void)?

       @discardableResult

       @objc optional static func routerControlable(rootVC: UIViewController?, param: Any?, closure: ((Any)->Void)?)->Self

 

       // MARK: Param - UIViewController?, Any?, ((Any)->(Any))?

       @discardableResult

       @objc optional static func routerControlable(rootVC: UIViewController?, param: Any?, returnClosure: ((Any)->(Any))?)->Self

    }

 

 --> **UIViewController 路由使用案例**:

 註冊綁定:

    @objc protocol WisdomProtocolLeftVCProtocol {}

    class WisdomProtocolLeftVC: UIViewController, WisdomRegisterable, WisdomProtocolLeftVCProtocol {

       // 註冊綁定協議/UIViewController

       static func registerable() -> WisdomClassable {

           return WisdomClassable(register: WisdomProtocolLeftVCProtocol.self, conform: Self.self)

       }

    }

協議實現:

     extension WisdomProtocolLeftVC: WisdomRouterControlable {

       // 實現路由協議

       @discardableResult

       static func routerControlable(rootVC: UIViewController?, param: Any?) -> Self {

           let vc = Self.init()

           vc.modalPresentationStyle = .fullScreen

           rootVC?.navigationController?.pushViewController(vc, animated: true)

           return vc

       }

    }

 

--> 外部開始路由:

    // 通過註冊到協議,獲取到UIViewController

    // MARK: WisdomRouterControlable 路由控制器-無參數

    let vcClass = WisdomProtocol.getRouterControlable(from: LeftVCProtocol.self)

    // 創建對象

    _=vcClass?.routerControlable?(rootVC: self, param: nil)

 【3】. UIView 路由協議: 

    // MARK: - Router UIView Protocol

    @objc public protocol WisdomRouterViewable where Self: UIView  {

       // MARK: Param - UIView?, Any?

       @discardableResult

       @objc optional static func routerViewable(superview: UIView?, param: Any?)->Self

 

       // MARK: Param - UIView?, Any?, ((Any)->Void)?

       @discardableResult

       @objc optional static func routerViewable(superview: UIView?, param: Any?, closure: ((Any)->Void)?)->Self

 

       // MARK: Param - UIView?, Any?, ((Any)->(Any))?

       @discardableResult

       @objc optional static func routerViewable(superview: UIView?, param: Any?, returnClosure: ((Any)->(Any))?)->Self

    }

--> **UIView 路由使用案例**:

註冊協議:

    @objc protocol WisdomProtocolLeftVIProtocol {}

    class WisdomProtocolLeftVI: UIView, WisdomRegisterable, WisdomProtocolLeftVIProtocol {

       // 註冊綁定協議/UIView

       static func registerable() -> WisdomClassable {

           return WisdomClassable(register: WisdomProtocolLeftVIProtocol.self, conform: Self.self)

       }

    }

實現協議:

    extension WisdomProtocolLeftVI: WisdomRouterViewable {

        // 實現路由協議

        static func routerViewable(superview: UIView?, param: Any?) -> Self {

           let vi = Self.init()

           // 添加/佈局視圖

           if let supervi = superview {

               supervi.addSubview(vi)

               vi.snp.makeConstraints { make in

                   make.top.equalTo(supervi).offset(10)

                   make.bottom.equalTo(supervi).offset(-20)

                   make.left.equalTo(supervi).offset(30)

                   make.right.equalTo(supervi).offset(-30)

              }

           }

           return vi

       }

    }

--> 外部開始路由:

     // 通過註冊到協議,獲取到UIView

    // MARK: WisdomRouterViewable 路由UIView-無參數

    let viClass = WisdomProtocol.getRouterViewable(from: LeftVIProtocol.self)

    // 創建對象

    let viewable=viClass?.routerViewable?(superview: self.view, param: nil)

 

 【4】. Param 路由協議:

    // MARK: - Router Param Protocol

    @objc public protocol WisdomRouterParamable {

       // MARK: Param - Any?

       @objc optional func routerParamable(param: Any?)

 

       // MARK: Param - Any?, ((Any)->Void)?

       @objc optional func routerParamable(param: Any?, closure: ((Any)->Void)?)

 

       // MARK: Param - Any?, ((Any)->(Any))?

       @objc optional func routerParamable(param: Any?, returnClosure: ((Any)->(Any))?)

    }

 

--> **Param 路由使用案例**:

實現協議:

    extension WisdomProtocolLeftVI: WisdomRouterParamable{

       // 接收外部到路由

       func routerParamable(param: Any?) {

           if let colorDic = param as? [String:Any] {

               model = WisdomProtocolLeftModel.decodable(value: colorDic)

               paramLabel.text = " 6. 參數路由代碼示例:\n\n (1). WisdomProtocolLeftVI 需實現協議: \n -- WisdomRouterParamable 參數路由協議\n\n // MARK: 調用 路由參數方法\nlet param = ['bgColor':bgColor,'textColor':textColor,'codeColor':codeColor]\n (self?.viewable as? WisdomRouterParamable)?.routerParamable?(param: param)\n"

           }

       }

    }

 

---> 外部開始路由:

    // MARK: WisdomRouterViewable 路由UIView-無參數

    let viClass = WisdomProtocol.getRouterViewable(from: LeftVIProtocol.self)

    viewable=viClass?.routerViewable?(superview: self.view, param: nil)

    DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+2) { [weak self] in

       // MARK: WisdomRouterParamable 參數路由協議

       let param = ["bgColor":"708069","textColor":"FFFFFF","codeColor":"33A1C9"]

       // 可選解包,路由參數

       (self?.viewable as? WisdomRouterParamable)?.routerParamable?(param: param)

    }

 

### 以上就是SDK路由核心功能介紹,完成

如果您熱衷於iOS/swift開發,是一位熱愛學習進步的童鞋,歡迎來一起研究/討論 開發中遇到的問題。聯繫QQ:497609288 。

我會繼續我的創作。

 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章