Skip to content

Instantly share code, notes, and snippets.

@muyexi
Last active December 20, 2018 08:00
Show Gist options
  • Save muyexi/2e189fc8fc558f286783e11de8e8bbd3 to your computer and use it in GitHub Desktop.
Save muyexi/2e189fc8fc558f286783e11de8e8bbd3 to your computer and use it in GitHub Desktop.
Implicit retain cycle in lazy var
class API {
var status: String?
var failureHandler: (() -> Void)?
var completionHandler: (() -> Void)?
init() {}
}
class ViewController: UIViewController {
lazy var api: API = {
let api = API()
api.completionHandler = {
}
api.failureHandler = {
// `api` reference Cause retian cycle
// Use `self.api` instead of 'api' to fix it
print(api.status ?? "")
}
return api
}()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment