Swift 3.1
NSInvocation
can be used dynamically, but only as a fun exercise, definitely not for serious applications. There are better alternatives.
import Foundation
class Test: NSObject {
@objc var name: String? {
didSet {
NSLog("didSetCalled")
}
}
func invocationTest() {
// This is the selector we want our Invocation to send
let namePropertySetterSelector = #selector(setter:name)
// Look up a bunch of methods/impls on NSInvocation
let nsInvocationClass: AnyClass = NSClassFromString("NSInvocation")!
// Look up the "invocationWithMethodSignature:" method
let nsInvocationInitializer = unsafeBitCast(
method_getImplementation(
class_getClassMethod(nsInvocationClass, NSSelectorFromString("invocationWithMethodSignature:"))!
),
to: (@convention(c) (AnyClass?, Selector, Any?) -> Any).self
)
// Look up the "setSelector:" method
let nsInvocationSetSelector = unsafeBitCast(
class_getMethodImplementation(nsInvocationClass, NSSelectorFromString("setSelector:")),
to:(@convention(c) (Any, Selector, Selector) -> Void).self
)
// Look up the "setArgument:atIndex:" method
let nsInvocationSetArgAtIndex = unsafeBitCast(
class_getMethodImplementation(nsInvocationClass, NSSelectorFromString("setArgument:atIndex:")),
to:(@convention(c)(Any, Selector, OpaquePointer, NSInteger) -> Void).self
)
// Get the method signiture for our the setter method for our "name" property.
let methodSignatureForSelector = NSSelectorFromString("methodSignatureForSelector:")
let getMethodSigniatureForSelector = unsafeBitCast(
method(for: methodSignatureForSelector)!,
to: (@convention(c) (Any?, Selector, Selector) -> Any).self
)
// ObjC:
// 1. NSMethodSignature *mySignature = [self methodSignatureForSelector: @selector(setName:)];
// 2. NSInvocation *myInvocation = [NSInvocation invocationWithMethodSignature: mySignature];
// 3. [myInvocation setSelector: @selector(setName:)];
// 4. [myInvocation setArgument: @"new name", atIndex: 2];
// 5. [myInvocation invokeWithTarget: self];
// 1.
let namyPropertyMethodSigniature = getMethodSigniatureForSelector(self, methodSignatureForSelector, namePropertySetterSelector)
// 2.
let invocation = nsInvocationInitializer(
nsInvocationClass,
NSSelectorFromString("invocationWithMethodSignature:"),
namyPropertyMethodSigniature
) as! NSObject // Really it's an NSInvocation, but that can't be expressed in Swift.
// 3.
nsInvocationSetSelector(
invocation,
NSSelectorFromString("setSelector:"),
namePropertySetterSelector
)
var localName = "New name" as NSString
// 4.
withUnsafePointer(to: &localName) { stringPointer in
nsInvocationSetArgAtIndex(
invocation,
NSSelectorFromString("setArgument:atIndex:"),
OpaquePointer(stringPointer),
2
)
}
// 5.
invocation.perform(NSSelectorFromString("invokeWithTarget:"), with: self)
}
}
let object = Test()
object.invocationTest()
NSInvocation
and you shouldn't, even in objective-C. One possible solution are named closures in a dictionary but even that is a bit of a code smell. – TuftsNSInvocation
. You just need to make sure all the classes and selectors are marked with@objc
. – Chassidychassin#selector
syntax but there is no such syntax forNSInvocation
. Closures are just safer. There are some other aspects related to memory management since the name of the method in Obj-C affects that. In summary, this is the old way of doing things, we have better and safer alternatives now. – Tufts