Use Swift's proxy, closures to encapsulate a common protocol to reduce spam codes

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.