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

时间:2014-06-25 04:00:22

标签: objective-c foundation variadic-functions

我跟随Stephen Kochan的Objective-C编程,第6版,我希望对这种特殊方法有所帮助。基本上我有一个类AddressBook,它有两个实例变量:一个名为NSMutableArray的{​​{1}}和一个名为book的{​​{1}}。相当不言自明。

NSString持有bookName,我正在尝试实现一个方法,该方法接受book类型的多个变量,并将所有变量添加到数组AddressCard。这是:

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方法的自定义实现。问题是,我提供给NSMutableArray的第一个参数没有添加到addObject。这是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,但实际上并没有被添加,正如输出所证明的那样。怎么了?

2 个答案:

答案 0 :(得分:1)

在您的代码中

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);
}

答案 1 :(得分:1)

使用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];
    }
}