我正在尝试从plist文件中创建一个对象数组(animalImages),该文件包含要从我的资源文件夹中提取的图像的名称。我尝试使用for循环单独添加每个图像但在某种程度上迷失在逻辑中。这就是我所拥有的:
NSString *images = [[NSBundle mainBundle] pathForResource:@"images" ofType:@"plist"]; //string with resource path
animalImageNames = [[NSMutableArray alloc] initWithContentsOfFile:images]; //array with file names
int i;
int j = 10;
for (i=0; i <=j; i++) {
animalImages = [[NSArray alloc] initWithObjects:
[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"%@.png",[animalImageNames objectAtIndex:i]]]];
}
我想,一旦我得到答案,我可能会打我的头,但我只是对这段代码的initWithImage部分中的操作顺序感到困惑。
答案 0 :(得分:4)
NSString *images = [[NSBundle mainBundle] pathForResource:@"images" ofType:@"plist"];
// need the names for just now. creating an autoreleased one
// and no need to be mutable
NSArray *animalImageNames = [NSArray arrayWithContentsOfFile:images];
// this array will contain the images
// need to release this when you are done
NSMutableArray *animalImages = [[NSMutableArray alloc] init];
// loop through all names from the plist and add image to the array
for (NSInteger i = 0; i < [animalImageNames count]; i++) {
NSString *name = [animalImageNames objectAtIndex:i];
[animalImages addObject:[UIImage imageNamed:[NSString stringWithFormat:@"%@.png", name]]];
}
可能有一些小的错误,因为我还没有编译代码。