組件化
- 下圖為我設(shè)想中的組件化流程圖:
![組件化流程圖.png](http://upload-images.jianshu.io/upload_images/1832614-e3369469075058ed.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
- 業(yè)務(wù)模塊層
- 組件化的目標(biāo)是為了實現(xiàn)不同業(yè)務(wù)模塊之間的獨立性,從而使業(yè)務(wù)開發(fā)更專注于單個模塊.
我傾向于利用cocoapods私有庫管理多個模塊 - 優(yōu)勢: - 可以使用pod方便集成 - 可以使用tag進(jìn)行版本切換 一些阻礙: - cocoapods私有庫只能依賴公有庫,所以必須將基礎(chǔ)組件層暴露出來(既要暴露出來!當(dāng)然要大家一起用啦!!) - 一些共用自定義UI組件,這部分經(jīng)過劃分后歸到業(yè)務(wù)模塊調(diào)用 - 基礎(chǔ)的項目環(huán)境文件(定制的網(wǎng)絡(luò)接口/數(shù)據(jù)解析之類的),只能通過建立模板私有庫的方式解決(一旦更新就很絕望,幸好這部分不常變...)
路由方案 - SPRoutable :Github傳送門
gif.gif
-
示例
-
建立目標(biāo)類用于接受Routable調(diào)用
- 目標(biāo)類為swift,且存在于Frameworks中,則需要在頭部添加@objc(類名)
- 目標(biāo)類只會init一次,除非手動清除
- 類名與函數(shù)名具有默認(rèn)前綴(可自行配置)
- swift示例:
@objc(Router_swift) class Router_swift: NSObject { var flag = true func router_a(params:[String: Any]) -> UIViewController { let vc = UIViewController() vc.view.backgroundColor = UIColor.blue return vc } func router_b() -> UIView { let view = UIView() view.frame = CGRect(x: 0, y: 300, width: 300, height: 300) view.backgroundColor = flag ? UIColor.red : UIColor.blue flag = !flag return view } func router_c(params: [String: Any] = [:]) { let alert = UIAlertController() alert.title = #function alert.message = params.description let action = UIAlertAction(title: "確定", style: UIAlertActionStyle.cancel, handler: nil) alert.addAction(action) UIApplication.shared.keyWindow?.rootViewController?.present(alert, animated: true, completion: nil) } }
- 可以這樣使用:
get viewController: guard let vc = Routable.viewController(url: "http://swift/a") else { return } get view: guard let v = Routable.view(url: "http://swift/b") else { return } get object: guard let v: UIView = Routable.object(url: "http://swift/b") else { return } get function: Routable.executing(url: str)
-
-
路由配置參數(shù)配置:
Routable.classPrefix = "Router_" // defalut: "Router_" Routable.funcPrefix = "router_" // defalut: "router_" Routable.paramName = "Params" // defalut: "Params" Routable.scheme = "scheme" // defalut: ""