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
@objc_block doesn't seem to exist in Swift 4. Any idea what the syntax was changed to?