swift学习笔记

1.在 Swift 编程中,最吸引我的就是能在文件中创建多个扩展。这使得我可以把互相关联的方法放在一起。比如每次我向控制器添加一个新协议时,就可以把这个协议的方法放在同一个扩展中。同理,TableView 相关的私有样式初始化方法或者私有 cell 初始化方法都可以放入各自的扩展中。


import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var tableView: UITableView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        styleNavigationBar()
    }
}

private typealias TableViewDataSource = ViewController
extension TableViewDataSource: UITableViewDataSource {
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 5
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("identifier", forIndexPath: indexPath)
        return cell
    }
}

private typealias TableViewDelegate = ViewController
extension TableViewDelegate: UITableViewDelegate {
    
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 64.0
    }
}

private typealias ViewStylingHelpers = ViewController
private extension ViewStylingHelpers {
    
    func styleNavigationBar() {
        // style navigation bar here
    }
}

按照上面的写法 就能优雅的命名对应的扩展

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

推荐阅读更多精彩内容

  • Swift属性 Swift属性将值跟特定的类,结构体,枚举关联。分为存储属性和计算属性,通常用于特定类型的实例。属...
    小小厨师阅读 875评论 0 0
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,251评论 4 61
  • Swift2.0 1.defer译为延缓、推迟之意类似栈 注意作用域,其次是调用顺序——即一个作用域结束(注意),...
    zeqinjie阅读 3,410评论 0 50
  • 每天给自己力量的 不是梦想 而是幻想
    打不死的小花阅读 289评论 0 0
  • (前言) 傍晚的海边,天边橙红色的霞云追随着夕阳的方向卷去,像爱人一样的缠缠绵绵。 太阳带着它的炽热之情,渐渐西沉...
    胥浅阅读 2,996评论 13 10