Swift5: App模块化

开发者 2024-9-22 16:20:19 80 0 来自 中国

目标是将序列化和网络哀求做成单独的framework而且可以更换.
全部公用的东西都放在公共库Common中.
workspace 'AppDemo.xcworkspace'platform :ios, '13.0'use_frameworks!load './Api/api_v1.rb'target :Common do  project 'Common/Common.xcodeproj'  pod 'Alamofire' endtarget :AppDemo do    project 'App/AppDemo/AppDemo.xcodeproj'    pod 'Common', :path => './Common/'        load_api_podsendtarget :ApiModelSerialize do    project 'Api/ApiModelSerialize/ApiModelSerialize.xcodeproj'    pod 'Common', :path => './Common/'    pod 'SwiftyJSON'endtarget :ApiNetwork do    project 'Api/ApiNetwork/ApiNetwork.xcodeproj'    pod 'Common', :path => './Common/'    pod 'Alamofire'endload_api_pods是一个单独.rb文件, 想更换framework的话就修改这个文件.
def load_api_pods    pod 'ApiModelSerialize', :path => './Api/ApiModelSerialize/'    pod 'ApiNetwork', :path => './Api/ApiNetwork/'end序列化(ApiModelSerialize)

在ApiModelSerialize这个framework中声明一个protocol
protocol ISwiftyJson {    init()        mutating func dFromJSON(json: JSON)    func dToJSON() -> JSON}再为一些根本范例扩展依照一下这个protocol
extension Int: ISwiftyJson {    mutating func dFromJSON(json: JSON) {        self = json.intValue    }        func dToJSON() -> JSON {        var json = JSON()        json.intValue = self        return json    }}必要序列化的Model也必要扩展依照一下这个protocol
extension Member: ISwiftyJson {        public func dFromJSON(json: JSON) {        Id = json["Id"].intValue    }    public func dToJSON() -> JSON {        var json = JSON()        json["Id"].intValue = Id        return json;    }}如今序列化这个是根本上算是实现了, 但是还必要封装一下.
Common中声明一个protocol
public protocol IApiModelSerialize {    mutating func aLoadFromJSONString<T>(model: T, str: String)    func aToJSONString<T>(model: T) -> String    func aConvertJSONStringToArray<T>(type: T.Type, str: String) -> [T] where T: ServerModel}ApiModelSerialize中依照一下这个protocol
public class PApiModelSerialize: IApiModelSerialize {    public init() {            }        public func aLoadFromJSONString<T>(model: T, str: String) {        if var m = model as? ISwiftyJson {            m.dFromJSON(json: JSON(parseJSON: str))        }    }        public func aToJSONString<T>(model: T) -> String {        if let m = model as? ISwiftyJson {            return m.dToJSON().rawString()!        }        fatalError("Can't deSerialize")    }        public func aConvertJSONStringToArray<T>(type: T.Type, str: String) -> [T] where T : IServerModel {        let arrayValue = JSON(parseJSON: str)        var array = [T]()        for j in arrayValue {            if let jsonString = j.1.rawString() {                var t = T()                t.aLoadFromJSONString(str: jsonString)                array.append(t)            }        }        return array    }}Common中声明一个protocol
public protocol IServerModel {    init()        mutating func aLoadFromJSONString(str: String)    func aToJSONString() -> String}ServerModel作为全部Model的父类依照这个protocol
open class ServerModel: IServerModel {    public required init() {            }        open func aLoadFromJSONString(str: String) {        Api.pModelSerialize.aLoadFromJSONString(model: self, str: str)    }        open func aToJSONString() -> String {        return Api.pModelSerialize.aToJSONString(model: self)    }}Model序列化的时间调用aLoadFromJSONString这个方法就可以了.
网络哀求(ApiNetwork)

Common中声明一个protocol
public protocol IApiNetwork {    func aRequest<T>(_ params: ServerParam, handler: IServerRequestHandler<T>?) where T: IServerModel}ApiNetwork中依照一下这个protocol
public class PApiNetwork: IApiNetwork {    public init() {            }        public func aRequest<T>(_ params: ServerParam, handler: IServerRequestHandler<T>?) where T : IServerModel {        ServerUtil_Alamofire.fRequest(params, handler: handler)    }}实际利用中调用一下就可以了
open class ServerUtil_Account {    private static let c_Controller = "Account"        public static func Login(loginParam: LoginParam, handler: IServerRequestHandler<MyProfile>?) {        let param = ServerParam(post: true)            .controller(c_Controller)            .action("Login")            .add(value: loginParam)        Api.pNetwork.aRequest(param, handler: handler)    }}关联声明与实现

ApiModelSerialize和ApiNetwork声明在Common中, 实如今详细framework中, 在App中必要将他们关联起来.
class AppConfigImpl {    init() {    }        func connect() {        Api.pModelSerialize = PApiModelSerialize()        Api.pNetwork = PApiNetwork()    }}class AppDelegate: UIResponder, UIApplicationDelegate {        var window: UIWindow?        func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {        AppConfigImpl().connect()        return true    }}末了用登录功能做一下验证
        ServerUtil_Account.Login(loginParam: loginParam, handler: IServerRequestHandler<MyProfile>({data in            print(data.Member?.NickName)        }, fail: { (failInfo) in                    }))代码

代码放在github, 有必要自取.
https://github.com/drenhart/AppDemo.git
您需要登录后才可以回帖 登录 | 立即注册

Powered by CangBaoKu v1.0 小黑屋藏宝库It社区( 冀ICP备14008649号 )

GMT+8, 2024-10-18 20:28, Processed in 0.137511 second(s), 32 queries.© 2003-2025 cbk Team.

快速回复 返回顶部 返回列表