繁体   English   中英

使用for循环中的数据填充NSMutableArray

[英]populate NSMutableArray with data from for loop

我正在尝试执行与NSMutableArray相关的看似简单的代码,但遇到了麻烦。 这是我的代码:

NSMutableArray *newArray = [[[NSMutableArray alloc] initWithCapacity:4] retain];
NSArray *existingSection2 = [[NSArray alloc] initWithObjects:@"S2 Item1",@"S2 Item2",@"S2 Item3",@"S2 Item4",nil];

for (int i=0; i<[existingSection2 count]; i++) {
    NSString *val = [[NSString alloc] initWithString:[existingSection2 objectAtIndex:i]];
    [newArray addObject:val];
    NSLog(@"val %i is %@ new array now contains: %@",i,val,[newArray objectAtIndex:i]);
    NSLog(@"new array description: ",[newArray description]);
}
NSLog(@"what's in newArray: ",[newArray objectAtIndex:0]);

据我了解,这是我在做什么:
1)分配一个名为newArray的新NSMutableArray,容量为4
2)用四个NSString值分配一个名为existingSection2的NSArray
3)遍历existingSection2中的四个NSString中的每一个
3a)在位置i处分配一个名为val的NSString与existingSection2数组的内容
3b)在下一个可用位置将val添加到newArray
3c)记录一些调试信息
4)记录newArray的最终内容以进行调试

这段代码在application:didFinishLaunchingWithOptions:但是这是我在模拟器中启动时控制台窗口显示的内容:

2011-06-26 15:50:48.203 ArrayTest[14936:207] val 0 is S2 Item1 new array now contains: S2 Item1
2011-06-26 15:50:48.205 ArrayTest[14936:207] new array description: 
2011-06-26 15:50:48.205 ArrayTest[14936:207] val 1 is S2 Item2 new array now contains: S2 Item2
2011-06-26 15:50:48.205 ArrayTest[14936:207] new array description: 
2011-06-26 15:50:48.206 ArrayTest[14936:207] val 2 is S2 Item3 new array now contains: S2 Item3
2011-06-26 15:50:48.206 ArrayTest[14936:207] new array description: 
2011-06-26 15:50:48.206 ArrayTest[14936:207] val 3 is S2 Item4 new array now contains: S2 Item4
2011-06-26 15:50:48.206 ArrayTest[14936:207] new array description: 
2011-06-26 15:50:48.207 ArrayTest[14936:207] what's in newArray: 

我不明白为什么我不用这四个新的NSString对象填充newArray! 我一直在寻找类似的问题,但答案似乎都与未初始化NSMutableArray有关,我认为我做得很好。

谢谢你的帮助!

您有一些我想指出的问题,希望您可以从中学到一些东西。 开始了...

内存管理

当使用allocnewcopy创建对象时,例如[[NSArray alloc] ...][[NSString alloc] ...] ,该对象将返回+1保留计数。 这意味着您“拥有”该对象,并负责稍后通过调用release (您当前未在代码中进行此操作)来释放它。 例如,如果使用[NSArray arrayWith...] ,那么您将不会获得+1保留计数(您会获得一个自动释放的对象),并且如果您想retain它,则必须调用retain

就是说,当你做

NSMutableArray *newArray = [[[NSMutableArray alloc] initWithCapacity:4] retain];

alloc给您+1保留计数,然后您第二次调用retain 这是不必要的。

但是,由于不需要在此函数之外使用数组或字符串,因此根本不需要保留它们,因此可以使用:

NSMutableArray *newArray = [NSMutableArray arrayWithCapacity:4];

NSString *val = [NSString stringWithString:[existingSection2 objectAtIndex:i]];
// or even more simply
NSString *val = [existingSection2 objectAtIndex:i];

有关更多详细信息和最佳做法,请阅读《 内存管理编程指南》

循环/迭代

您在for循环中使用[existingSection2 count]i++的方法for可行的,但这并不是最好的方法。 Obj-C具有所谓的“ 快速枚举” ,它很简单,我希望您可以使用它:

for (NSString *val in existingSection2) {
    //...
}

NSLog和格式化字符串

NSLog函数的工作方式(类似于C的printf )通过采用格式字符串(其中包含格式说明符,例如%d用于整数, %@用于Obj-C对象),然后输入要用于说明符的参数列表。

使用时

NSLog(@"new array description: ",[newArray description]);

您将只记录字符串“ new array description:”,因为您没有与该描述相对应的格式说明符。 相反,您应该这样做:

NSLog(@"new array description: %@", [newArray description]);
// or even more simply
NSLog(@"new array description: %@", newArray);

您还可以在NSLog上使用有关字符串格式说明符的更多信息。

总体

这是使用我上面描述的技术重写的代码。

NSMutableArray *newArray = [NSMutableArray arrayWithCapacity:4];
NSArray *existingSection2 = [NSArray arrayWithObjects:@"S2 Item1",@"S2 Item2",@"S2 Item3",@"S2 Item4",nil];

for (NSString *val in existingSection2) {
    [newArray addObject:val];
    NSLog(@"value is %@",val);
    NSLog(@"new array contains: %@", newArray);
}
NSLog(@"what's in newArray: %@",[newArray objectAtIndex:0]);

打印阵列说明时,需要在格式字符串中添加%@

另外,您不需要显式调用description因为在使用%@打印对象时会自动调用description ,如下所示:

NSLog(@"New array is: %@", newArray);

您有记录问题:

NSLog(@"new array description: ",[newArray description]);

应该

NSLog(@"new array description: %@",[newArray description]);

奖金帮助:

NSMutableArray *newArray = [[NSMutableArray alloc] initWithCapacity:4];  // fix memory leak
NSArray *existingSection2 = [[NSArray alloc] initWithObjects:@"S2 Item1",@"S2 Item2",@"S2 Item3",@"S2 Item4",nil];

for (id section in existingSection2) // fast enumeration
{
    NSString *val = [[NSString alloc] initWithString:[existingSection2 objectAtIndex:i]];
    [newArray addObject:val];
    NSLog(@"val %i is %@ new array now contains: %@",i,val,[newArray objectAtIndex:i]);
    NSLog(@"new array description: %@",[newArray description]);
    [val release]; // fix memory leak
}

NSLog(@"what's in newArray: %@",[newArray objectAtIndex:0]);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM