我无法让 forwardInvocation 工作。出于某种原因,Objective-C 运行时完全忽略了我的 forwardInvocation: 方法并抛出了一个无法识别的选择器异常。
我的测试代码如下:
@interface InvocationTest : NSObject
{
}
+ (void) runTest;
@end
@interface FullClass: NSObject
{
int value;
}
@property(readwrite,assign) int value;
@end
@implementation FullClass
@synthesize value;
@end
@interface SparseClass: NSObject
{
}
@end
@implementation SparseClass
- (void)forwardInvocation:(NSInvocation *)forwardedInvocation
{
NSLog(@"ForawrdInvocation called");
FullClass* proxy = [[[FullClass alloc] init] autorelease];
proxy.value = 42;
[forwardedInvocation invokeWithTarget:proxy];
}
@end
@implementation InvocationTest
+ (void) runTest
{
SparseClass* sparse = [[[SparseClass alloc] init] autorelease];
NSLog(@"Value = %d", [sparse value]);
}
@end
我正在处理以下资源中的信息:
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtForwarding.html#//apple_ref/doc/uid/TP40008048-CH105 http://cocoawithlove.com/2008/ 03/construct-nsinvocation-for-any-message.html
据我所知,当我调用 [sparse value] 时,运行时应该在 SparseClass 的实例上调用 forwardInvocation:,但它被完全忽略:
-[SparseClass 值]:无法识别的选择器发送到实例 0x4b1c4a0 *** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[SparseClass 值]:无法识别的选择器发送到实例 0x4b1c4a0”