Skip to content

Instantly share code, notes, and snippets.

@JoshuaSullivan
Last active December 24, 2015 22:53
Show Gist options
  • Save JoshuaSullivan/34cdb4315d79b0a1d85d to your computer and use it in GitHub Desktop.
Save JoshuaSullivan/34cdb4315d79b0a1d85d to your computer and use it in GitHub Desktop.
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