Swift weakSelf in closure syntax

前端 未结 2 1320
栀梦
栀梦 2021-02-06 12:46

I have this code to get JSON:

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


        
相关标签:
2条回答
  • 2021-02-06 13:19

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

    You can see the documentation here

    0 讨论(0)
  • 2021-02-06 13:26

    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/

    0 讨论(0)
提交回复
热议问题