如何将Dictionary插入数组中

时间:2013-05-06 18:00:08

标签: ios objective-c xcode cocoa-touch

我是iOS和Objective-c的新手。 我正在尝试在我的应用中自动生成pList,看起来像这样。

enter image description here

到目前为止,我已经能够创建文件,使其成为普通的Value => Key文件,如果我用

替换我的for循环
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];

}

1 个答案:

答案 0 :(得分:5)

你拥有的是一系列词典。伪代码显示结构:

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

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