Swift weakSelf in closure syntax

前端 未结 2 1350
栀梦
栀梦 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条回答
  •  萌比男神i
    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/

提交回复
热议问题