メソッドを動的に呼び出すには、NSInvocation を使用する必要があります。ここで私が試したこと:
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[[messageRecord.senderController class] instanceMethodSignatureForSelector:messageRecord.receiverAction]];
[invocation setSelector:messageRecord.receiverAction];
[invocation setTarget: messageRecord.senderController];
[invocation setArgument: &(message.data) atIndex:2];
[invocation invoke];
messageRecord.senderController
はメソッドが呼び出されるオブジェクトであり、messageRecord.receiverAction
このコードに指定されたセレクターであることに言及する必要があります。また、message.data
タイプ (NSArray *) のオブジェクトであり、適切に初期化されています。
このコードでは、次のコンパイル時エラーが発生します
Address of property expression requested
呼び出しプロセスを次のように変更すると、期待どおりに動作します。
NSArray *dataArray = message.data;
[invocation setSelector:messageRecord.receiverAction];
[invocation setTarget: messageRecord.senderController];
[invocation setArgument: &dataArray atIndex:2];
[invocation invoke];
2 つの唯一の違いは、ローカル NSArray ポインターを作成し、それに message.data を割り当てたことです。message.data
後で、それ自体ではなく、新しく作成されたポインターのアドレスを指定しました。
なぜそれが機能したのですか?とにかく違いは何ですか?