Skip to content

Instantly share code, notes, and snippets.

@drewcrawford
Created April 27, 2015 18:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drewcrawford/ef7834d3369fa889bcfd to your computer and use it in GitHub Desktop.
Save drewcrawford/ef7834d3369fa889bcfd to your computer and use it in GitHub Desktop.
import Foundation
let notification = "MyNotification"
struct DontCopy {
var value = false
mutating func whatever(#callback:() -> ()) {
self.value = true
NSNotificationCenter.defaultCenter().addObserverForName(notification, object: nil, queue: nil) { (noti) -> Void in
self.value = false
callback()
}
}
}
var dc = DontCopy()
assert(dc.value == false)
dc.whatever { () -> () in
println("Value in callback is \(dc.value)")
}
assert(dc.value == true)
NSNotificationCenter.defaultCenter().postNotificationName(notification, object: nil)
println("Value out of callback is \(dc.value)")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment