Skip to content

Instantly share code, notes, and snippets.

@eonist
Forked from SwiftStudies/Swift Swizzle
Created March 13, 2017 23:05
Show Gist options
  • Save eonist/40ed63b10944b677e15b3a3e39a734cf to your computer and use it in GitHub Desktop.
Save eonist/40ed63b10944b677e15b3a3e39a734cf to your computer and use it in GitHub Desktop.
import Cocoa
// Only needed until we have class variables
var __SwizzleSayHello = { (who:String) -> String in
return "Hello, \(who)"
}
class Swizzle {
//Only needed until we have class variables
class var _sayHello : (String)->String { get{ return __SwizzleSayHello } set (swizzle) {__SwizzleSayHello = swizzle} }
func sayHello(who:String)->String{
return Swizzle._sayHello(who)
}
}
let immutableInstance = Swizzle()
var mutableInstance = Swizzle()
//Both print "Hello, World"
println(immutableInstance.sayHello("World"))
println(mutableInstance.sayHello("World"))
Swizzle._sayHello = { (who:String) -> String in
return "Howdy, \(who)"
}
//Both print "Howdy, World"
println(immutableInstance.sayHello("World"))
println(mutableInstance.sayHello("World"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment