具有可变参数的Objective-C方法-跳过第一个参数



我正在学习Stephen Kochan的《Programming in Objective-C》第6版,我希望在这个特殊的方法上得到一些帮助。基本上,我有一个类AddressBook,它有两个实例变量:称为bookNSMutableArray和称为bookNameNSString。很明显。

book持有AddressCard s,我试图实现一种方法,该方法接受id类型的多个变量,并将它们全部添加到数组book中。

-(void) addCards:(id)firstCard, ... NS_REQUIRES_NIL_TERMINATION
{
    va_list argumentList;
    va_start(argumentList, firstCard);
    id theArgument = firstCard;
    while ((theArgument = va_arg(argumentList, id)))
    {
        [self addCard:theArgument];
    }
    va_end(argumentList);
}

addCard方法是NSMutableArrayaddObject方法的自定义实现。问题是,我提供给addCards的第一个参数没有添加到book。下面是main.m和相应的输出:

int main(int argc, const char * argv[])
{
    @autoreleasepool
    {
        AddressCard *card1 = [[AddressCard alloc] initWithName:@"Joseph Brown" andEmail:@"jbrown@yahoo.com"];
        AddressCard *card2 = [[AddressCard alloc] initWithName:@"Thomas Walter" andEmail:@"t.walter@gmail.com"];
        AddressCard *card3 = [[AddressCard alloc] initWithName:@"Jonathan Green" andEmail:@"jon_green@gmail.com"];
        AddressCard *card4 = [[AddressCard alloc] initWithName:@"Elizabeth White" andEmail:@"elizwhite@live.com"];
        AddressBook *myBook = [[AddressBook alloc] initWithName:@"My Address Book"];
        [myBook addCards:card1, card2, card3, card4, nil];
        NSLog(@"Lookup: Joseph Brown");
        if ([myBook lookup:@"joseph Brown"] == nil)
        {                                   
            NSLog(@"Not Found!");
        }
        else
        {                                   
            [[myBook lookup:@"Joseph Brown"] print];
        }
        [myBook list];
    }
    return 0;
}
输出:

2014-06-25 11:43:25.997 AddressBook[19454:303] Lookup: Joseph Brown
2014-06-25 11:43:25.999 AddressBook[19454:303] Not Found!
2014-06-25 11:43:25.999 AddressBook[19454:303] ======== Contents of My Address Book ========
2014-06-25 11:43:26.000 AddressBook[19454:303] Thomas Walter           t.walter@gmail.com              
2014-06-25 11:43:26.000 AddressBook[19454:303] Jonathan Green          jon_green@gmail.com             
2014-06-25 11:43:26.000 AddressBook[19454:303] Elizabeth White         elizwhite@live.com              
2014-06-25 11:43:26.001 AddressBook[19454:303] =============================================
Program ended with exit code: 0

lookup正是这样做的:查找由参数指定的AddressCard;print以一种很好的格式打印AddressCard;list也有同样的功能:列出book的内容。现在,从输出中可以看到,Joseph Brown虽然作为card1添加到AddressBook中,但实际上并没有添加,正如输出所证明的那样。怎么了?

使用NSArray的替代方案…

AddressCard *card1 = [[AddressCard alloc] initWithName:@"Joseph Brown" andEmail:@"jbrown@yahoo.com"];
AddressCard *card2 = [[AddressCard alloc] initWithName:@"Thomas Walter" andEmail:@"t.walter@gmail.com"];
AddressCard *card3 = [[AddressCard alloc] initWithName:@"Jonathan Green" andEmail:@"jon_green@gmail.com"];
AddressCard *card4 = [[AddressCard alloc] initWithName:@"Elizabeth White" andEmail:@"elizwhite@live.com"];
AddressBook *myBook = [[AddressBook alloc] initWithName:@"My Address Book"];
[myBook addCards:@[card1, card2, card3, card4]];

-(void)addCards:(NSArray *)cards {
    for (AddressCard *card in cards) {
        [self addCard:card];
    }
}

在你的代码中

id theArgument = firstCard;
while ((theArgument = va_arg(argumentList, id)))

你没有对firstCard做任何事情。第二个参数覆盖它

-(void) addCards:(id)firstCard, ... NS_REQUIRES_NIL_TERMINATION
{
    va_list argumentList;
    va_start(argumentList, firstCard);
    id theArgument = firstCard;
    while (theArgument)
    {
        [self addCard:theArgument];
        theArgument = va_arg(argumentList, id);
    }
    va_end(argumentList);
}

相关内容

  • 没有找到相关文章

最新更新