2009-02-27 4 views
0

개체 모음이 포함 된 클래스가 있습니다. 제공된 술어와 일치하는 콜렉션의 첫 번째 멤버를 리턴하는 메소드를 작성하려고합니다.인식 할 수없는 선택기가 아직 디버그에서 볼 수 있습니다.

... 
//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.value == 1)"줄에 내가 "알 수없는 선택이 클래스에 보내"얻고 있다는 것입니다 : 다음은 시험 방법 플러스 술어이다. 이해가되지 않는 것은 디버거가 값 속성과 값을 볼 수 있으므로 객체에 해당 선택 항목이 명확하게 표시됩니다. 아이디어가 있으십니까?

은 BTW 리프 함수 정의 표기 프로토콜을

답변

1

결국 문제가 발견되었습니다. 그것은이 라인을

[myInvocation setArgument:thisNotation atIndex:2]; 

했다가 덜 혼란 변수 명명 전략을 선택하는

3

오타 구현?

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

당신이 얻고 있다는 사실

,없는 경우 "인식 할 수없는 선택기 클래스로 전송" leafClass 것을 의미한다. 확인해야 할 두 가지 사항은 다음과 같습니다.

  • 리프의 초기화 프로그램 initWithValue이 개체를 반환하는지 확인하십시오.
  • addNotation:이 Leafs를 배열에 올바르게 추가하는지 확인하십시오.
+0

좋은 인수

[myInvocation setArgument:&thisNotation atIndex:2]; 

감사하고 있어야합니다. – danielpunkass

+0

사실 그것은 오타가 stackoverflow 코드를 transposing에서 발생했습니다. 그들은 원래 이름이 아닙니다. 나는 그 포스트를 고치겠다. – Ian1971

관련 문제