雨燕3.1
NSInvocation
可以動態地使用,而只是作爲一個有趣的練習,絕對不是重要的應用程序。有更好的alternatives。
class Test : NSObject
{
var name : String? {
didSet {
NSLog("didSetCalled")
}
}
func invocationTest() {
let invocation : NSObject = unsafeBitCast(method_getImplementation(class_getClassMethod(NSClassFromString("NSInvocation"), NSSelectorFromString("invocationWithMethodSignature:"))),to:(@convention(c)(AnyClass?,Selector,Any?)->Any).self)(NSClassFromString("NSInvocation"),NSSelectorFromString("invocationWithMethodSignature:"),unsafeBitCast(method(for: NSSelectorFromString("methodSignatureForSelector:"))!,to:(@convention(c)(Any?,Selector,Selector)->Any).self)(self,NSSelectorFromString("methodSignatureForSelector:"),#selector(setter:name))) as! NSObject
unsafeBitCast(class_getMethodImplementation(NSClassFromString("NSInvocation"), NSSelectorFromString("setSelector:")),to:(@convention(c)(Any,Selector,Selector)->Void).self)(invocation,NSSelectorFromString("setSelector:"),#selector(setter:name))
var localName = name
withUnsafePointer(to: & localName) { unsafeBitCast(class_getMethodImplementation(NSClassFromString("NSInvocation"), NSSelectorFromString("setArgument:atIndex:")),to:(@convention(c)(Any,Selector,OpaquePointer,NSInteger)->Void).self)(invocation,NSSelectorFromString("setArgument:atIndex:"), OpaquePointer($0),2) }
invocation.perform(NSSelectorFromString("invokeWithTarget:"), with: self)
}
}
你試圖解決什麼問題? – Alexander
我很抱歉,但我正在爲客戶工作。但我會盡力描述它。我必須在配置文件的幫助下配置一個視圖,該配置文件可以根據需要進行配置。也就是說,可能的配置列表(每個嘗試訪問許多不同的方法)都很大。是的,他們可以按情況進行管理(將案例映射到方法),但我試圖使用通用解決方案來嘗試和管理此問題。 – Aswath
這種代碼的動態性通常是糟糕的體系結構(沒有違法)的結果。有一些非常特殊的例外情況,但在95%的情況下,您不需要使用'NSInvocation',即使在Objective-C中也不應該使用。一種可能的解決方案是在字典中命名爲閉包,但即使這樣也有點代碼味道。 – Sulthan