我想在我的SZNUnmanagedReference类上使用消息转发。它具有以下属性:
@property (nonatomic, strong) NSSet *authors;
@property (nonatomic, strong) SZNReferenceDescriptor *referenceDescriptor;
基本上,当UnmanagedReference的实例收到消息authorsString
时,它应该将其转发到referenceDescriptor
,其中有一个名为- (NSString *)authorsStringWithSet:(NSSet *)authors
的方法。
所以,我在SZNUnmanagedReference.m
中写了这个:
- (void)forwardInvocation:(NSInvocation *)anInvocation {
SEL aSelector = anInvocation.selector;
if ([NSStringFromSelector(aSelector) isEqualToString:NSStringFromSelector(@selector(authorsString))]) {
NSMethodSignature *signature = [self.referenceDescriptor methodSignatureForSelector:@selector(authorsStringWithSet:)];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
NSSet *authors = [NSSet setWithSet:self.authors];
[invocation setSelector:@selector(authorsStringWithSet:)];
[invocation setArgument:&authors atIndex:2];
[invocation setTarget:self.referenceDescriptor];
[invocation invoke];
} else {
[self doesNotRecognizeSelector:aSelector];
}
}
- (BOOL)respondsToSelector:(SEL)aSelector {
if ([super respondsToSelector:aSelector]) {
return YES;
} else if ([NSStringFromSelector(aSelector) isEqualToString:NSStringFromSelector(@selector(authorsString))] && [self.referenceDescriptor respondsToSelector:@selector(authorsStringWithSet:)]) {
return YES;
} else {
return NO;
}
}
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector {
NSMethodSignature *signature = [super methodSignatureForSelector:aSelector];
if (!signature) {
signature = [self.referenceDescriptor methodSignatureForSelector:@selector(authorsStringWithSet:)];
}
return signature;
}
这一切似乎都有效,SZNReferenceDescriptor
类中的代码会被执行。
但是,我不知道如何让authorsString
回来。如果我正确理解文档,我认为referenceDescriptor
应该将结果发送回消息的原始发件人。但它似乎没有用。在我的测试课程中,[unmanagedReference authorsString]
会返回nil
。
答案 0 :(得分:8)
问题是你正在构造一个新的NSInvocation
对象,其返回值在需要它的位置是不可访问的(消息调度“stack”的“顶部”)。运行时只知道它为你创建的那个(forwardInvocation:
的参数;那是它将使用它的返回值的那个。所有你需要做的就是设置它的返回值:
- (void)forwardInvocation:(NSInvocation *)anInvocation {
if (anInvocation.selector == @selector(authorsString)) {
id retVal = [self.referenceDescriptor authorsStringWithSet:self.authors];
[anInvocation setReturnValue:&retVal]; // ARC may require some memory-qualification casting here; I'm compiling this by brain at the moment
} else {
[super forwardInvocation:anInvocation];
}
}
实际上,实际上没有必要创建新的调用;因为你需要的只是方法的返回值,你可以直接发送消息(如果你刚刚在authorsString
上实现了SZNUnmanagedReference
,你也可以这样做,而不是使用转发机制。)< / p>
另外,请注意,不需要将选择器转换为字符串以及从字符串转换选择器以进行比较 - SEL
可以使用相等运算符直接进行比较。