Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
How to use imp_implementationWithBlock in Swift
import Foundation
import ObjectiveC.runtime
let myString = "foobar" as NSString
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)
Copy link

cprovatas commented Nov 2, 2017

@objc_block doesn't seem to exist in Swift 4. Any idea what the syntax was changed to?

Copy link

@cprovatas Use @convention(block) instead of @objc_block

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