5

I have this code to get JSON:

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

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

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

2 Answers 2

11

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
}

However take a note that you are not creating retain cycle here, so you do not have to use weak or unowned self here. Good article on this topic: http://digitalleaves.com/blog/2015/05/demystifying-retain-cycles-in-arc/

1
  • Thank you! And for link too.
    – Maria
    May 13, 2015 at 7:02
2

You can declare a weak self reference by putting [weak self] before your closure parameters.

You can see the documentation here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.