Jon*_*ics 1 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]);
Run Code Online (Sandbox Code Playgroud)
根据我的理解,这就是我正在做的事情:
1)分配一个名为newArray的新NSMutableArray,容量为4
2)分配一个名为existingSection2的NSArray,其中包含四个NSString值
3)迭代现有部分2中的四个NSStrings中的每一个.3a
)分配一个名为val的NSString,其中包含在现有位置i的existingSection2数组的内容
.3)在下一个可用位置添加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:
Run Code Online (Sandbox Code Playgroud)
我不明白为什么我没有用这四个新的NSString对象填充newArray!我一直在寻找类似的问题,但答案似乎都与初始化NSMutableArray有关,我相信我做得正确.
谢谢你的帮助!
你有一些我想指出的问题,我希望你能从中学到一些东西.开始了...
当您使用创建对象alloc,new或者copy,如[[NSArray alloc] ...]或[[NSString alloc] ...],它返回一个+1保留计数.这意味着您"拥有"该对象,并负责稍后通过调用release(您当前未在代码中执行)释放它.例如,如果您使用,[NSArray arrayWith...]则不会获得+1保留计数(您将获得一个自动释放的对象),如果您希望它保持不变,则必须调用retain.
那说,当你这样做
NSMutableArray *newArray = [[[NSMutableArray alloc] initWithCapacity:4] retain];
Run Code Online (Sandbox Code Playgroud)
该alloc给你一个+1保留计数,然后你调用retain第二次!这是不必要的.
但是,由于您不需要在此函数之外使用数组或字符串,因此您根本不需要保留它们,因此您可以使用:
NSMutableArray *newArray = [NSMutableArray arrayWithCapacity:4];
Run Code Online (Sandbox Code Playgroud)
和
NSString *val = [NSString stringWithString:[existingSection2 objectAtIndex:i]];
// or even more simply
NSString *val = [existingSection2 objectAtIndex:i];
Run Code Online (Sandbox Code Playgroud)
有关更多详细信息和最佳实践,请阅读" 内存管理编程指南".
您在循环中使用[existingSection2 count]和使用的方法有效,但不是最好的方法.Obj-C有一个叫做Fast Enumeration的东西,它简单得多,我希望你能用它:i++for
for (NSString *val in existingSection2) {
//...
}
Run Code Online (Sandbox Code Playgroud)
该NSLog函数printf通过获取格式字符串(包含%d整数和%@Obj-C对象的格式说明符),然后是为说明符输入的参数列表,起作用(类似于C ).
当你使用
NSLog(@"new array description: ",[newArray description]);
Run Code Online (Sandbox Code Playgroud)
您将只记录字符串"new array description:",因为您没有与描述相对应的格式说明符.相反,你应该这样做:
NSLog(@"new array description: %@", [newArray description]);
// or even more simply
NSLog(@"new array description: %@", newArray);
Run Code Online (Sandbox Code Playgroud)
还可以在字符串格式说明符上获得更多可用于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]);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4354 次 |
| 最近记录: |