使用 extension 让你的代码更有条理

你的 ViewController 非常混乱吗? 在 Swift 的世界里,有一种非常简单的魔法可以让你的 ViewController 变得条理分明。

如果你不知道什么叫做 extension, 那你应该去看看 Swift 教程。

简单来说,extension就是一个类的扩展,在 Objective-C 的世界里,它叫 Category。

为什么使用 extension 可以使我们的代码变得更有条理,请看代码。

假如我们有一个 UIViewController, 它控制着一个 UICollectionView,那么 UIViewController 也遵丛 UICollectionViewDataSource, UICollectionViewDelegate 两个协议,不使用 extension 的话,我们会这样写代码。

class TopicDetailMembersViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if let count = eventHandler.membersInteractor.topicItem?.members.count {
            return count
        }
        else {
            return 0
        }
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath)
        return cell
    }
    
}

但其实,我们可以让事情变得更有趣一些,使用 extension 可以这样写

class TopicDetailMembersViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }

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

// MARK: - UICollectionViewDataSource, UICollectionViewDelegate
extension TopicDetailMembersViewController: UICollectionViewDataSource, UICollectionViewDelegate {
    
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if let count = eventHandler.membersInteractor.topicItem?.members.count {
            return count
        }
        else {
            return 0
        }
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath)
        return cell
    }
    
}

区分只是将一部分的代码移到另一个代码块中,代码量少的时候可能效果不明显,但当一个 ViewController 中充斥大量类似代码的时候,效果拔群。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,259评论 4 61
  • 有一天,群里一位朋友发了一张照片。说真的,没有经过任何修图,我看起来也很舒服。是她自己穿着文胸的照片,她是要来推介...
    mimi播报阅读 691评论 2 6
  • 邹老师住院了,几天不上语文课的感觉,实在很爽。好景不长,昨天的语文课,新的语文老师出现了。 新老师是什么时候走进教...
    小坏蛋格瑞特阅读 481评论 0 0
  • 根据维基百科,拟人化偏误指的是”人用基于人类的模式来推测非人类个体的特性以获得关于环境更高效的判断,即使这些推测并...
    曹鹤元阅读 2,639评论 0 1
  • 那些喜欢你的总有一天会不喜欢你,那些你抓紧的总有一天会抓不住,那些你想实现的梦想也许根本实现不了,那些曾经以为无比...
    依陌阅读 537评论 0 0