3

次のコードは期待どおりに機能します。

NSLog(@"%@", [NSString stringWithString:@"test"]; // Logs "test"

しかし、それを に置き換えるとNSInvocation、まったく異なる結果が得られます。

Class class = [NSString class];
SEL selector = @selector(stringWithString:);

NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:
                          [class methodSignatureForSelector:selector]];
[invocation setTarget:class];
[invocation setSelector:selector];
[invocation setArgument:@"test" atIndex:2];
[invocation invoke];

id returnValue = nil;
[invocation getReturnValue:&returnValue];
NSLog(@"%@", returnValue); // Logs "NSCFString"

高低を検索しましたが、これを理解できません。何か助けはありますか?ありがとう!

4

1 に答える 1

7

NSInvocationクラスリファレンスから:

引数値がオブジェクトの場合、オブジェクトのコピー元の変数(またはメモリ)へのポインタを渡します。

NSArray *anArray;    
[invocation setArgument:&anArray atIndex:3];

@ "test"は実際にはNSStringのインスタンスを構築しているため、次を使用する必要があります

NSString *testString = @"test";
[invocation setArgument:&testString atIndex:2];
于 2011-03-07T19:45:35.173 に答える