美文网首页
swift 基础知识

swift 基础知识

作者: 壹点微尘 | 来源:发表于2018-01-02 20:14 被阅读8次
    cmd + ctrl + e 替换相同对象名字
    cmd + shift + o 搜索查找
    使用 guard 语法, 依次判断每一项是否有值, 只要有一项为 nil, 就不再执行后续的代码!
    guard let pty = list?[i],
               let cName = property_getName(pty),
               let name = String(utf8String: cName)
              else{
                        // 这个guard 在for循环里面,不能写return
                        // 继续遍历下一个
                        continue
    }
    
    在闭包中调属性, 需要用self.调用
    loadData { (list) in
                print(list)
                // `拼接`数组, 闭包中定义好的代码, 在需要的时候执行, 需要 self. 指定语境 
                self.personList += list
                // 刷新表格
     }
    

    类型转换 as
    SwiftString 之外, 绝大部分使用as 需要? / !
    as! / as? 直接根据前面的返回值来决定
    注意: if let / guard let 判空语句, 一律使用 as?

    let vc = segue.destination as! DetailViewController
            
    // 设置选种的 person, indexPath
    if let indexPath = sender as? IndexPath {
          // indexPath 一定有值
          vc.person = personList[indexPath.row]
    }
    
    闭包回调传值
      1. 声明一个闭包属性
    //闭包是可选的
    var completionCallBack: (()->())?
    
    • 2.调用闭包传值
    // 执行闭包回调
    // OC 中执行block前都必须判断是否有值, 否则容易崩溃
    // ! 强行解包 (Xcode 帮助修订, 一定不要用 `!`)
    // ? 可选解包 -> 如果 闭包为 nil, 就什么也不做
    completionCallBack?()
    

    相关文章

      网友评论

          本文标题:swift 基础知识

          本文链接:https://www.haomeiwen.com/subject/csavgxtx.html