[英]Swift weakSelf in closure syntax
我有這個代碼來獲取JSON:
Alamofire.request(.GET, worlds).responseJSON { (request, response, JSON, error) in
println(JSON)
//weakSelf.serverList = JSON
}
如何在這里申報weakSelf? 我知道在我的情況下它應該是無主的,但我找不到正確的語法。 當我嘗試使用[unowned self] .serverList而不是注釋行時,編譯器顯示錯誤“使用未解析的標識符'unowned'”。 我也嘗試在塊之前聲明常量,如下所示:
unowned let uSelf = self
它就像一個魅力,但我想了解如何在我的情況下使用[無主自我]。
使用捕獲列表。 正確的語法是:
Alamofire.request(.GET, worlds).responseJSON { [unowned self] (request, response, JSON, error) in
println(JSON)
self.serverList = JSON
}
但請記住,您不是在這里創建保留周期,因此您不必在此處使用weak
或unowned
。 關於這個主題的好文章: http : //digitalleaves.com/blog/2015/05/demystifying-retain-cycles-in-arc/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.