Skip to content

Instantly share code, notes, and snippets.

@SwiftStudies
Created July 14, 2014 11:47
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save SwiftStudies/e3cf1debe970f4a22b3e to your computer and use it in GitHub Desktop.
Save SwiftStudies/e3cf1debe970f4a22b3e 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"))
@eonist
Copy link

eonist commented Mar 13, 2017

This is awesome. Method swizzle!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment