Ben Five study on the protocol in Ios:swift and its application examples

Source: Internet
Author: User

Recently in learning iOS crawl Web content encountered a variety of duplicate code problems, looking at so many duplicate code, feel really uncomfortable, so learning iOS protocol.

In my opinion, this protocol should actually be the interface in Java.

That is, define a protocol (or interface), define a bunch of methods, and let an instance of the class that implements the method pass in.


In my project, crawl different pages of the Viewcontroller, get the code of the page is exactly the same, in addition to the different URLs, indistinguishable, so this part extracted as a class, specifically to achieve the Internet access to data, data taken to the need to notify Viewcontroller, At this point a callback is needed, which is used to achieve this goal in protocol.


First, define a protocol.

@objc protocol Callback:nsobjectprotocol {    func success (data:string)    optional func error (ERROR:NSERROR)}

The protocol here is called CallBack, which defines two methods, one of which is optional.

Then define a class implementation to crawl the Web page

Class Netutil:nsobject, nsurlconnectiondelegate,nsurlconnectiondatadelegate{var cb:callback? Init (url:string,cb:callback) {super.init () SELF.CB = cb Let Req:nsurlrequest = Nsurlrequest (url:n        sURL (String:url)!)    Let conn:nsurlconnection = Nsurlconnection (Request:req, delegate:self)!    } var data:nsmutabledata! var Tabledata:nsarray = Nsarray () Func connection (connection:nsurlconnection, Didreceivedata data:nsdata) {s    Elf.data.appendData (data); } Func Connection (connection:nsurlconnection, Didreceiveresponse response:nsurlresponse) {self.data = NSMutab    Ledata (); } func connectiondidfinishloading (connection:nsurlconnection) {Let S = NSString (Data:data, Encoding:nsut f8stringencoding) CB?.    Success (s!) } Func Connection (connection:nsurlconnection, Didfailwitherror error:nserror) {cb?. Error? (Error)}}

I call it the Network tool, because the use of nsurlconnection to fetch data, so the same class needs to implement the Nsurlconnection two protocol, and then in the corresponding method call callback method to notify the main program to complete the corresponding work.

Call Cb.success () after success, call Cb.error () after an error.

These are all ready, then go back to the corresponding Viewcontroller to implement this callback protocol, and implement the call.

  viewcontroller.swift//  Test Engineering////  Created by weeks Honey on 14/11/26.//  Copyright (c) 2014 www.miw.cn. All rights Reserved.//import Uikitclass viewcontroller:uiviewcontroller,callback{    override func Viewdidload () {        super.viewdidload ()        var url:string! = "Http://www.miw.cn/info/csdn/cloud/1/list"        //Call My Network tool here, Self as a parameter that implements the protocol callback        Netutil (Url:url, cb:self)    }    @IBOutlet weak var appstableview:uitableview!    Override Func didreceivememorywarning () {        super.didreceivememorywarning ()    }    //This implements the necessary methods in callback    func Success (data:string) {        println (data)    }    }

Does it seem that my main program is much simpler now?

It is possible to process the data in the corresponding success method.

Ben Five study on the protocol in Ios:swift and its application examples

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.