Skip to content

Instantly share code, notes, and snippets.

@fukaoi
Created April 27, 2015 13:47
Show Gist options
  • Save fukaoi/426856f4fca63c86a947 to your computer and use it in GitHub Desktop.
Save fukaoi/426856f4fca63c86a947 to your computer and use it in GitHub Desktop.
Use closure of stored dictionary data
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