Swift weakSelf在闭包语法 [英] Swift weakSelf in closure syntax

查看:1214
本文介绍了Swift weakSelf在闭包语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码来获得JSON:

I have this code to get JSON:

Alamofire.request(.GET, worlds).responseJSON { (request, response, JSON, error) in
        println(JSON)
        //weakSelf.serverList = JSON
    }

如何在这里声明weakSelf?我知道它应该是在我的情况下没有所有,但我找不到正确的语法。当我尝试使用[unowned self] .serverList而不是注释行,编译器显示我的错误使用未解决的标识符'unowned'。
我也尝试在块之前声明常量,像这样:

How to declare weakSelf here? I know it should be unowned in my case, but I can't find correct syntax for this. When I try use [unowned self].serverList instead of the commented line, the compiler shows me error "use of unresolved identifier 'unowned'". I also tried to declare constant before block like this:

unowned let uSelf = self

它的作用就像一个魅力,但我想了解如何使用[unowned self]

It works like a charm, but I want to understand how to use [unowned self] in my case.

推荐答案

使用捕获列表。正确的语法是:

Use the capture list. The correct syntax is:

Alamofire.request(.GET, worlds).responseJSON { [unowned self] (request, response, JSON, error) in
    println(JSON)
    self.serverList = JSON
}


b $ b

但是请注意,你不是在这里创建retain循环,所以你不必使用 weak unowned self here。关于此主题的好文章: http://digitalleaves.com/ blog / 2015/05 / demystifying-retain-cycles-in-arc /

这篇关于Swift weakSelf在闭包语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆