In iOS development, if you do not properly encapsulate, use protocols or inherit classes to develop, you will encounter the legendary Viewcontroller (hereafter referred to as VC) Hell problem ...
For example, in order to invoke the interface in the network app, we make a simple judgment, there will be the following garbage code (legacy of Predecessors):
OverrideFuncViewdidload() {Super.viewdidload ()var color =Uicolor (red:153/255, Green:204/255, Blue:204/255, Alpha:1)Self.navigationcontroller?. Navigationbar.bartintcolor = ColorSelf.httpController.delegate =SelfConfig.shareinstance (). isnetworkrunning =Checknetwork.doesexistencenetwork ()IfConfig.UUID = =Nil | |Config.Uuid!. IsEmpty {Tool.showerrorhud ("Go to the information portal to log in:)"}Elseif!Config.shareinstance (). isnetworkrunning {Tool.showerrorhud ("You don't seem to be connected")}else {Tool.showprogresshud ("Updating campus Network Information") Sendnicapi ()}Do any additional setup after loading the view.}FuncSendnicapi(){Let Nicurl ="Http://herald.seu.edu.cn/api/nic"Let parameter:Nsdictionary = ["UUID":config. uuid!] self.httpcontroller.posttourlaf (Nicurl, Parameter:parameter, tag: "Nic")} func didreceivedicresults (results:nsdictionary, tag:string) {if let Content:nsdictionary = Results[ "content"] as? nsdictionary{if tag = = "Nic" { Firstsend = false tool.showsuccesshud ( "Get information success") println (Content.allkeys)}}}
See, each VC begins to write this way, if we have more than 20 functions? What would it be like?
List of features of the pit Daddy
So, it's absolutely not going to work, you have to initialize the entire mess, send requests, request acceptance for encapsulation, and use Swift's most useful protocols, proxies, and closures.
This is first through the protocol and the agent, the closure is placed in the next article.
Protocols, as the name implies, are interfaces within other languages (C + + 's abstract classes are similar)
Because Swift does not support the normal type (int) is set to static, the class method if it is static, you must add the Class keyword (I think this is a bit of a slot), Only structs and enums can be used directly with static (and small tip can be used to wrap a struct into a normal type, set as a calculation type, and then act as a static member, but not here)
We can first encapsulate a simple initialization method like this ...
ClassFuncInitnavigationapi (Vc:Uiviewcontroller,Navbarcolor:Uicolor)Httpcontroller? {var Httpcontroller:Httpcontroller =Httpcontroller ()Vc.navigationcontroller?. Navigationbar.bartintcolor = NavbarcolorConfig.shareinstance (). isnetworkrunning =checknetwork.doesexistencenetwork () if config. uuid = = nil | | config. uuid!. isempty{tool.showsuccesshud ( "Please fill your information in Sidebar profile")} else if! Config.shareinstance (). isnetworkrunning{tool.showerrorhud ( " Looks like you don't have a connection. ")} else{tool.showprogresshud ( "getting Information") return Httpcontroller} return nil}
Ok? That Httpcontroller is another interface, to perform network operations, the agent needs to rely on it, so we return a Httpcontroller instance, if the failure to return nil, in the actual VC add a packet to judge.
Later, you want to initialize, that's all you need.
OverrideFuncViewdidload() {Super.viewdidload ()var color = uicolor (red: 153/255, Green: 204/255, Blue: 204/255, Alpha: 1) Self.httpcontroller = tool.initnavigationapi (self,navBarColor : color)?? nil if (self.httpController! = " Span class= "Hljs-literal" >nil) {self.httpcontroller!. Delegate = self tool.sendapi ( " Carddetail ", Httpcontroller: self.httpcontroller!)}}
Throw away a bunch of messy code. The next step is to use the agent to proxy all our requests and the corresponding results, the next article to fill in ...
Use Swift's proxy, closures to encapsulate a common protocol to reduce spam codes