Home > Article > Web Front-end > Basic knowledge about swift
This time I will bring you the basic knowledge about swift. What are the precautions about the basic knowledge of swift. The following is a practical case, let’s take a look.
cmd + ctrl + e Replace the same object name
cmd + shift + o Search and find
Use guard syntax to determine whether each item has a value in turn. As long as one item is nil, the subsequent code will no longer be executed!
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 // 刷新表格 }
Type conversion as
Except String in Swift, most of the uses of as require? / !
as! / as? is determined directly based on the previous return value
Note: if let / guard let is used to detect null statements, always use as?
let vc = segue.destination as! DetailViewController // 设置选种的 person, indexPathif let indexPath = sender as? IndexPath { // indexPath 一定有值 vc.person = personList[indexPath.row] }
Closed callback value passing
Declare a closure attribute
//闭包是可选的var completionCallBack: (()->())?
2. Call the closure to pass the value
// 执行闭包回调// OC 中执行block前都必须判断是否有值, 否则容易崩溃// ! 强行解包 (Xcode 帮助修订, 一定不要用 `!`)// ? 可选解包 -> 如果 闭包为 nil, 就什么也不做completionCallBack?()
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website!
Related reading:
Detailed explanation of the use of Rxjs
Using CSS to implement text truncation in HTML
The above is the detailed content of Basic knowledge about swift. For more information, please follow other related articles on the PHP Chinese website!