小编Jon*_*ics的帖子

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

我正在尝试执行看似与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 …
Run Code Online (Sandbox Code Playgroud)

iphone objective-c ios

1
推荐指数
1
解决办法
4354
查看次数

标签 统计

ios ×1

iphone ×1

objective-c ×1