Last active
August 29, 2015 14:02
-
-
Save akisute/9b598cc8de998096548a to your computer and use it in GitHub Desktop.
Several disappointing Swift code snippets...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyClass { | |
var someValue:Int | |
func someMethod() -> Int { | |
return 42 | |
} | |
var callback:(Int) -> (Void) = {(value:Int) -> (Void) in | |
// Using self within a closure owned by self makes reference loop | |
self.someValue = self.someMethod() + value | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyClass { | |
var someValue:Int | |
func someMethod() -> Int { | |
return 42 | |
} | |
var callback:(Int) -> (Void) = {[unowned self] (value:Int) -> (Void) in | |
// self is specified as unowned in closure capture list [unowned self], | |
// so the following self is now unowned by this closure! | |
self.someValue = self.someMethod() + value | |
// By the way, cannot directly use variables and methods without self | |
// (the following code is compiler error) | |
/* | |
someValue = someMethod() + value | |
*/ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment