-
-
Save gatherheart/9dcba93ed5830055780e3b15abdd15b8 to your computer and use it in GitHub Desktop.
How to use imp_implementationWithBlock in Swift
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 Foundation | |
import ObjectiveC.runtime | |
let myString = "foobar" as NSString | |
println(myString.description) | |
let myBlock : @objc_block (AnyObject!) -> String = { (sself : AnyObject!) -> (String) in | |
"✋" | |
} | |
let myIMP = imp_implementationWithBlock(unsafeBitCast(myBlock, AnyObject.self)) | |
let method = class_getInstanceMethod(myString.dynamicType, "description") | |
method_setImplementation(method, myIMP) | |
println(myString.description) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment