スウィフト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'を使用する必要はなく、対話型であってもCであってはいけません。 1つの可能な解決策は、辞書でクロージャーという名前が付けられていますが、それは少しコード臭です。 – Sulthan