iphone - 用 for 循环中的数据填充 NSMutableArray

标签 iphone objective-c ios

我正在尝试执行与 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 中的四个 NSStrings 中的每一个
3a) 分配一个名为 val 的 NSString,其中包含位置 i 处的 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 相关,我相信我做得对。

谢谢你的帮助!

最佳答案

你有一些问题,我想指出,我希望你能从中学到一些东西。开始了...

内存管理

当您使用 alloc 创建对象时, new , 或 copy ,例如 [[NSArray alloc] ...][[NSString alloc] ...] ,它以 +1 保留计数返回。这意味着您“拥有”该对象并负责稍后通过调用 release 来释放它。 (您当前未在代码中执行此操作)。例如,如果您使用 [NSArray arrayWith...] ,那么你不会得到 +1 的保留计数(你会得到一个自动释放的对象),如果你想让它一直存在,你必须调用 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];

有关更多详细信息和最佳实践,请阅读 Memory Management Programming Guide .

循环/迭代

您的使用方法[existingSection2 count]i++for循环有效,但不是最好的做事方式。 Obj-C 有所谓的 Fast Enumeration ,这要简单得多,我希望你会使用它:
for (NSString *val in existingSection2) {
    //...
}

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

当你使用
NSLog(@"new array description: ",[newArray description]);

您将只记录字符串“新数组描述:”,因为您没有与描述相对应的格式说明符。相反,你应该这样做:
NSLog(@"new array description: %@", [newArray description]);
// or even more simply
NSLog(@"new array description: %@", newArray);

String Format Specifiers 上还有更多信息可用。您可以与 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]);

关于iphone - 用 for 循环中的数据填充 NSMutableArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6486390/

相关文章:

iphone - Core Data 中的轻量级迁移不能进行哪些更改?

ios - Sencha iOS 选择器错误

ios - 重新排序 UITableView 并隐藏重新排序控件

ios - 当用户使用打开的 Firebase FCM 调配关闭 iOS 设备设置中的通知时,如何实现应用程序行为?

iphone - 应用程序无法在设备或模拟器上启动

iphone - iOS 排行榜 : Rank users on overall shortest time

iphone - NSURLConnection 只工作一次

iphone - 在 UIPopover 中显示 ABPeoplePickerNavigationController

ios - CoreSpotlight 索引由于某种原因无法正常工作

objective-c - FBSDKLoginManager错误代码308并且单例无法解决它