如何将字典插入到数组中

3
我对iOS和Objective-C还很陌生。 我正在尝试在我的应用程序中自动生成一个类似于这样的pList文件。

enter image description here

到目前为止,如果我将我的for循环替换为以下内容,我已经成功创建了该文件,并使其成为普通的Value => Key文件。
for (NSString* exercisePictureName in bigPictureData) {

        [data setObject:exercisePictureName forKey:exercisePictureName];

    }

但我的问题是我不知道如何在循环结束时构建逻辑,以创建像图片中所示的文件结构。因为它必须是精确的。
有人可以指点我如何构建循环,以便创建正确格式的文件吗?
- (void) createImageListFromSource {


    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"exercisePictures.plist"];
    NSFileManager *fileManager = [NSFileManager defaultManager];

    if (![fileManager fileExistsAtPath: path])
    {
        path = [documentsDirectory stringByAppendingPathComponent: [NSString stringWithFormat: @"exercisePictures.plist"] ];
    }


    //To insert the data into the plist
    NSArray* bicepPictureData = [self getAllimagesThatStartWith:@"bicep-"];
    NSArray* tricepPictureData = [self getAllimagesThatStartWith:@"tricep-"];
    NSArray* absPictureData = [self getAllimagesThatStartWith:@"abs-"];
    NSArray* chestPictureData = [self getAllimagesThatStartWith:@"chest-"];
    NSArray* backPictureData = [self getAllimagesThatStartWith:@"back-"];

    NSArray* bigPictureData = [bicepPictureData arrayByAddingObjectsFromArray:tricepPictureData];
    bigPictureData = [bigPictureData arrayByAddingObjectsFromArray:absPictureData];
    bigPictureData = [bigPictureData arrayByAddingObjectsFromArray:chestPictureData];
    bigPictureData = [bigPictureData arrayByAddingObjectsFromArray:backPictureData];

    NSArray* finalData = [[NSArray alloc] init];
    for (NSString* exercisePictureName in bigPictureData) {

        NSDictionary* data = [[NSDictionary alloc] initWithObjectsAndKeys:exercisePictureName,@"text",exercisePictureName,@"image", nil];
        [finalData arrayByAddingObject:data];
        NSLog(@"%@",data);
    }
    NSLog(@"%@",finalData);
    [finalData writeToFile: path atomically:YES];

}

NSMutableArray 对于 finalData 来说是更好的选择... - holex
1
+1 为了展示所需的 plist 结构,提供漂亮的截图。 - matt
1个回答

5
您手头的是一个字典数组。以下是伪代码示例以显示其结构:

 NSMutableArray* arr = [NSMutableArray array];
 for (...) {
     NSDictionary* d = @{@"image": something, @"text": somethingelse};
     [arr addObject:d];
 }

当所有操作完成后,只需使用writeToURL...直接保存数组即可。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接