我正在学习Stephen Kochan的《Programming in Objective-C》第6版,我希望在这个特殊的方法上得到一些帮助。基本上,我有一个类AddressBook
,它有两个实例变量:称为book
的NSMutableArray
和称为bookName
的NSString
。很明显。
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
方法是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
虽然作为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);
}