懶加載控件
private lazy var tableView : UITableView = { [unowned self] in
let tableView = UITableView(frame: CGRectZero, style: .plain)
tableView.tg_width.equal(.fill)
tableView.tg_height.equal(.fill)
tableView.delegate = self
tableView.dataSource = self
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "12")
return tableView
}()
簡(jiǎn)單的一個(gè)閉包
func callBackEvent(url: String,
reponse:(_ result: String) ->()) {
let c1 = ""
reponse(c1)
}
類(lèi)的初始化
class person {
var name : String = "" /// 值類(lèi)型,初始化為空值
var age : Int = 0 /// 值類(lèi)型,初始化為空值
var myStore : store? /// 對(duì)象類(lèi)型,最好設(shè)置為可選類(lèi)型
}
可選鏈
協(xié)議
// 1. 協(xié)議標(biāo)記為 @objc,并繼承 NSObjectProtocol(可選方法必須的語(yǔ)法)
@objc protocol MyOptionalProtocol {
// 2. 強(qiáng)制實(shí)現(xiàn)的方法
func requiredMethod()
// 3. 使用 optional 關(guān)鍵字定義可選方法
@objc optional func optionalMethod()
@objc optional var optionalProperty: String { get }
}
// 4. 類(lèi)遵循協(xié)議(必須是 class 類(lèi)型)
class MyClass: NSObject, MyOptionalProtocol {
func requiredMethod() {
print("必須實(shí)現(xiàn)的方法")
}
// 可選方法/屬性可不實(shí)現(xiàn)
}
Result 的函數(shù)
func fetchData(completion: @escaping (Result<String, Error>) -> Void) {
// 模擬異步操作
DispatchQueue.global().async {
let isSuccess = Bool.random()
if isSuccess {
completion(.success("數(shù)據(jù)加載成功"))
} else {
completion(.failure(NSError(domain: "com.example.error", code: 404")))
}
}
}
/// 使用
fetchData { result in
switch result {
case .success(let data):
print("成功: \(data)")
case .failure(let error):
print("失敗: \(error.localizedDescription)")
}
}
cell 可以這種方式賦值
var entity : goods? {
didSet {
self.nameLabel.text = entity?.goods_name ?? ""
self.nameLabel.sizeToFit()
}
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。