看啥推荐读物
专栏名称: Chuck丶陈
高手之路,汇编起步。
目录
相关文章推荐
今天看啥  ›  专栏  ›  Chuck丶陈

Swift 项目实战

Chuck丶陈  · 简书  ·  · 2018-10-19 14:14

项目实用

  1. 存在的循环引用-容易被忽略的地方
  • Cell中, 可以实用 [weak self]
    // 在cell中实用闭包会存在循环引用的情况
     cell.callback = {[weak self] type in
                if self?.callback != nil {
                    self?.callback!(type, model)
                }
    }   
  • 在SB中跳转页面的时候实用闭包
      if segue.identifier == "selectmic" {
            let selectmic = segue.destination as! SelectMicController
            selectmic.callback = {[weak self] str in
                self?.micName.text = str
            }
            
       }
  • 使用EmptyDataSet的时候,
tableView.emptyDataSetView({[weak self] (view) in
                view.titleLabelString(NSAttributedString(string: "没有数据哟!"))
                    .didTapContentView { // 在此处使用weak self,还是会造成循环引用,self->tableView->emptyView.didTap->self
                        self?.tableView.mj_header.beginRefreshing()
                }
            })
  1. where 从句的用法,主要用于追加判断
// 1. 在if中, 忽略写法

if let model = UserManager.staned.userModel, model.id == "1" {
    // do somthing
}

// 2. 可以在for、do catch、switch中类似于if中一样追加判断
let tmp = (1, 2)

switch tmp {
case (let x, let y) where x > y: // 满足条件x>y才进入case 语句中
    // do somthing
default: break
}


// 在class、protocol、类似于在后面追加实现条件
// RxSwift 一个UIViewController 的扩展,在扩展中只有在UIViewController中实现该方法

extension Reactive where Base: UIViewController {

        /// Bindable sink for `title`.
        public var title: Binder<String> {
            return Binder(self.base) { viewController, title in
                viewController.title = title
            }
        }
    
}



原文地址:访问原文地址
快照地址: 访问文章快照