Created
April 27, 2015 13:47
-
-
Save fukaoi/426856f4fca63c86a947 to your computer and use it in GitHub Desktop.
Use closure of stored dictionary data
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
import UIKit | |
class Demo: NSObject { | |
func clojure() -> (index:Int) -> Dictionary<Int, Int> { | |
var dict = [:] as Dictionary<Int, Int> | |
var r = { (index:Int) -> Dictionary<Int, Int> in | |
dict[index] = index * 10 | |
return dict | |
} | |
return r | |
} | |
} | |
let demo = Demo() | |
let x = demo.clojure() | |
x(index: 1) | |
x(index: 2) | |
x(index: 3) | |
// [1: 10] | |
// [2: 20, 1: 10] | |
// [2: 20, 3: 30, 1: 10] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment