2009-02-27 17 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 Leafは表記プロトコルを実装しています

答えて

1

最終的に問題が見つかりました。 それはあまり混乱変数の命名戦略を選択するための良い引数

[myInvocation setArgument:&thisNotation atIndex:2]; 

おかげ

3

Typo in function definition?

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

あなたは、インスタンスではなく「クラスに送信され、認識されないセレクター」を取得しているという事実、leafClassであることを意味します。確認する2つの点:

  • リーフの初期設定子initWithValueがオブジェクトを返すようにしてください。
  • addNotation:がリーフをアレイに正しく追加していることを確認してください。
+0

されている必要があります

[myInvocation setArgument:thisNotation atIndex:2]; 

このラインでした。 – danielpunkass

+0

これは実際には、コードをstackoverflowに変換する際に誤植に過ぎませんでした。彼らは元の名前ではありません。私はポストを修正するつもりです – Ian1971

関連する問題