Last active
August 29, 2015 14:22
-
-
Save taku0/6e80abc401c3b80ea1e3 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 Sample1 { | |
| var count = 0 | |
| init() { | |
| println("Sample1 init") | |
| } | |
| deinit { | |
| println("Sample1 deinit") | |
| } | |
| func incrementCount() -> () -> Int { | |
| return { | |
| ++(self.count) | |
| } | |
| } | |
| } | |
| // これは循環参照する | |
| class Sample2 { | |
| var count = 0 | |
| var inc: (() -> Int)! = nil | |
| init() { | |
| println("Sample2 init") | |
| inc = incrementCount() | |
| } | |
| deinit { | |
| println("Sample2 deinit") | |
| } | |
| func incrementCount() -> () -> Int { | |
| return { | |
| ++(self.count) | |
| } | |
| } | |
| } | |
| // これも循環参照する | |
| class Sample3 { | |
| var count = 0 | |
| var sub: Sub! = nil | |
| init() { | |
| println("Sample3 init") | |
| sub = Sub(sample3: self) | |
| } | |
| deinit { | |
| println("Sample3 deinit") | |
| } | |
| func incrementCount() -> () -> Int { | |
| return { | |
| ++(self.count) | |
| } | |
| } | |
| } | |
| class Sub { | |
| let inc: () -> Int | |
| init(sample3: Sample3) { | |
| self.inc = sample3.incrementCount() | |
| } | |
| } | |
| func foo() { | |
| let inc1 = Sample1().incrementCount() | |
| let inc2 = Sample2().incrementCount() | |
| let inc3 = Sample3().incrementCount() | |
| println(inc1()) | |
| println(inc2()) | |
| println(inc3()) | |
| } | |
| foo() | |
| foo() | |
| foo() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment