2010-11-08 45 views
0

我想添加一個預定義的字符串到NSMutableArray的對象。我以爲你會使用%@,但顯然下面的代碼執行不好。謝謝你在前進將變量插入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]; 
+0

每當你做一個格式化字符串時,在它和你要替換的變量之間加逗號。在上面,你應該有...... @「你看起來很累」,名字...... – Luke 2010-11-26 00:23:40

回答

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]; 
+1

你不需要'無' - 終止'+ stringWithFormat:'參數... – kennytm 2010-11-08 14:03:31

+0

你是對的。我只是更改了示例代碼。謝謝 – pgb 2010-11-08 14:07:13