代码之家  ›  专栏  ›  技术社区  ›  Ian1971

无法识别的选择器,但调试可以看到它

  •  0
  • Ian1971  · 技术社区  · 17 年前

    以下是收集方法:

    ...
    //predicate is a boolean method that accepts an object as its single parameter
    -(id<Notation>) getFirstChildMatching: (SEL) predicate declaredInInstance:(id) instance
    {
        NSMethodSignature *sig = [[instance class] instanceMethodSignatureForSelector:predicate];
        NSInvocation *myInvocation = [NSInvocation invocationWithMethodSignature:sig];
        [myInvocation setTarget:instance];
        [myInvocation setSelector:predicate];
    
        int numItems = childNotations.count;
        for(int i=0;i< numItems;i++)
        {
            id<Notation> thisNotation = [childNotations objectAtIndex:i];
            [myInvocation setArgument:thisNotation atIndex:2];
            BOOL result =NO;
            [myInvocation retainArguments];
            [myInvocation invoke];
            [myInvocation getReturnValue:&result];
    
            if (result)
                return thisNotation;
    
        }
    
        return nil;
    }
    

    以下是测试方法和谓词:

    - (void) testGetFirstChildMatching
    {
        Leaf *line1 = [[Leaf alloc] initWithValue:1 step:Step_A andNumber:1];
        Leaf *line2 = [[Leaf alloc] initWithValue:2 step:Step_B andNumber:2];
    
        SEL mySelector = @selector(valueIs1:);
    
        id<CompositeNotation> compositeNotation = [[CompositeNotation alloc] init];
        [compositeNotation addNotation:line1];
        [compositeNotation addNotation:line2];
    
        id notation = [compositeNotation getFirstChildMatching: mySelector declaredInInstance:self];
        STAssertEquals(YES, [notation isKindOfClass:[Leaf class]], @"Should be of type Leaf: %@", notation);
        //Leaf *found = ((Leaf *)notation);
        STAssertEquals([notation value], line1.value, @"Should have found line 1 with value 1: actual %i", [notation value]);
        [line1 release];
        [line2 release];
    }
    
    -(BOOL) valueIs1: (Leaf *) leaf
    {
        if (leaf.value == 1)
            return YES;
    
        return NO;
    }
    

    有什么想法吗?

    顺便说一句,Leaf实现了表示法协议

    2 回复  |  直到 17 年前
        1
  •  3
  •   codelogic    17 年前

    函数定义中的输入错误?

    -(BOOL) valueIs1: (Leaf *) Leaf // <== should be "leaf" not "Leaf" ?
    

    班 “,而不是实例,意味着 leaf 是一个 Class

    • initWithValue
    • 确保 addNotation: 正在向数组中正确添加叶。
        2
  •  1
  •   Ian1971    17 年前

    我最终发现了问题所在。

    [myInvocation setArgument:thisNotation atIndex:2];
    

    [myInvocation setArgument:&thisNotation atIndex:2];
    

    谢谢