Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
An Exploration of Capture Semantics. Read the blog post: http://www.chibicode.org/?p=28
func attemptLogin(user: String, password: String) {
self.loginRequest = APIClient.sharedClient().createLoginRequest(user:user, password:password) {
result in
switch result {
case .Success(let data):
parseLoginData(data) // Compiler error: implicit reference to self
case .Failure(let error):
errorHandlingMethod(error) // Compiler error: implicit reference to self
}
}
}
func attemptLogin(user: String, password: String) {
self.loginRequest = APIClient.sharedClient().createLoginRequest(user:user, password:password) {
result in
switch result {
case .Success(let data):
self.parseLoginData(data)
case .Failure(let error):
self.errorHandlingMethod(error)
}
}
}
func attemptLogin(user: String, password: String) {
self.loginRequest = APIClient.sharedClient().createLoginRequest(user:user, password:password) {
[unowned self] result in
switch result {
case .Success(let data):
self.parseLoginData(data)
case .Failure(let error):
self.errorHandlingMethod(error)
}
}
}
func attemptLogin(user: String, password: String) {
self.loginRequest = APIClient.sharedClient().createLoginRequest(user:user, password:password) {
[weak self] result in
guard let strongSelf = self else { return }
switch result {
case .Success(let data):
strongSelf.parseLoginData(data)
case .Failure(let error):
strongSelf.errorHandlingMethod(error)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment