swift中extension的应用

http://blog.ios-developers.io/view-controller-extensions/

Say you are trying to make an iOS app that uses a UITableView. Along with the code that provides custom functionality, your view controller will need to conform to the UITableView data source and delegate in order to access methods that bring your views to life. This means including all of the protocol methods in your view controller.

// ViewController.swift
class ViewController: UIViewController {  
    override func viewDidLoad() {
        super.viewDidLoad()
        // Extra view initializations go here...
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    // DataSource
    func numberOfSectionsInTableView(tableview: UITableView) -> Int {
        return 1
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return Data.dataStore.count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)

        let row = indexPath.row
        cell.textLabel?.text = Data.dataStore[row]

        return cell
    }

    // Delegate
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        let row = indexPath.row
        print(Data.dataStore[row])
    }
}

Phew, there's a lot of code cluttering up the view controller, and it's really just there to conform to the protocols! Of course you can always just use some //colorfulcomments or MARK organization in order to add visual separation from the rest of your code. But what if there was a way to just move all of that code into its own file? Good news everyone, there is! All we need are some handy extensions.

Welcome to the Extension Zone

Extensions allow you to extend the functionality of a class, structure, enumeration, or protocol type. Say you wanted to add a custom method to the NSString class. Extensions let you do that. Or maybe you want to separate out some code, say in a view controller, into a different file. Extensions let you do that! Just take the code from before and bring it out into it's own file. This is demonstrated as follows.

ViewController.swift
class ViewController: UIViewController {  
    override func viewDidLoad() {
        super.viewDidLoad()
        // Extra view initializations go here...
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}
ViewController+DataSource.swift
extension ViewController: UITableViewDataSource {  
    func numberOfSectionsInTableView(tableview: UITableView) -> Int {
        return 1
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return Data.dataStore.count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)

        let row = indexPath.row
        cell.textLabel?.text = Data.dataStore[row]

        return cell
    }
}
ViewController+Delegate.swift
extension ViewController: UITableViewDelegate {  
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        let row = indexPath.row
        print(Data.dataStore[row])
    }
}

Voilà. Now all of the code related to the DataSource and Delegate protocols are contained within their own files. These methods are isolated from the rest of your view controller, yet they still provide the essential capabilities to your program. Just look at how clean that view controller is now!

One thing to make sure of though is that the delegate and data source for your tableview is assigned correctly. The easiest way to do this is through Interface Builder with the ctrl+drag functionality. Select the tableview and connect it to the parent view controller for both data source and delegate, like so...

SuperDuperAwesomesauce

Now you're all set. Sporting a sleek, slimmed down VC!


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值