Is it the right way using `[weak self]` in swift closure?

后端 未结 3 1315
醉梦人生
醉梦人生 2021-01-13 22:48

I always using [weak self] in swift closure to prevent reference cycle. Here is the code below, is it the correct way?

someTask(completion: {[w         


        
3条回答
  •  北海茫月
    2021-01-13 23:00

    You can use it like this from Swift 4.2

    someTask(completion: {[weak self] (result) in
        guard let self = self { return }
    
        //it safe when reach here always
    
        self.xxx = yyy
        self.doLongTermWork()
        self.finish()
    })
    

提交回复
热议问题