将变量插入NSMutableArray对象

时间:2010-11-08 13:59:05

标签: cocoa nsmutablearray iphone

我想在NSMutableArray的对象中添加一个预定义的字符串。我以为你会使用%@,但显然下面的代码执行得不好。提前谢谢

arrayData = [[NSMutableArray alloc]
               initWithObjects:@"%@ you look tired." name,
                               @"Why do you smell so bad?",
                               @"I have to go potty!",
                               @"%@ put your pants on!" name,
                               @"Mommy!",
                               @"Daddy!",
                               @"NOOOOOO!",
                               @"When are we going to get there?",
                               @"I HATE YOU!",
                               nil]; 

1 个答案:

答案 0 :(得分:5)

%@stringWithFormat:电话中有效。您的代码应如下所示:

arrayData = [[NSMutableArray alloc] initWithObjects:
                  [NSString stringWithFormat:@"%@ you look tired.", name],
                  @"Why do you smell so bad?",
                  @"I have to go potty!",
                  [NSString stringWithFormat:@"%@ put your pants on!", name],
                  @"Mommy!",
                  @"Daddy!",
                  @"NOOOOOO!",
                  @"When are we going to get there?",
                  @"I HATE YOU!",
                  nil];