5

我想在我的 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.

4

1 回答 1

8

问题是您正在构建一个新NSInvocation对象,它的返回值在需要它的位置(消息调度“堆栈”的“顶部”)不可访问。运行时只知道它为您创建的那个(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,而不是使用转发机制)。

另外,请注意,无需将选择器转换为字符串或从字符串转换来比较它们 -SEL可以使用相等运算符直接比较 s。

于 2012-07-12T18:26:51.707 回答