swift 基礎知識

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?()
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容