Swift weakSelf 使用
2020-11-11 本文已影响0人
凛冬将至2002
习惯了oc 的写法,在此记录一下swift 的写法。
第一种写法
request.responseString(encoding: NSUTF8StringEncoding) {[weak self] (res) -> Void in
if let strongSelf = self {
//do something...
}
}
第二种写法
weak var weakSelf = self
request.responseString(encoding: NSUTF8StringEncoding) {(res) -> Void in
if let strongSelf = weakSelf {
//do something
}
}