개체 모음이 포함 된 클래스가 있습니다. 제공된 술어와 일치하는 콜렉션의 첫 번째 멤버를 리턴하는 메소드를 작성하려고합니다.인식 할 수없는 선택기가 아직 디버그에서 볼 수 있습니다.
...
//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 리프 함수 정의 표기 프로토콜을
좋은 인수
감사하고 있어야합니다. – danielpunkass
사실 그것은 오타가 stackoverflow 코드를 transposing에서 발생했습니다. 그들은 원래 이름이 아닙니다. 나는 그 포스트를 고치겠다. – Ian1971