3

我对 iOS 和 Objective-c 很陌生。我正在尝试pList在我的应用程序中自动生成一个看起来像这样的。

在此处输入图像描述

到目前为止,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];

}
4

1 回答 1

5

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

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

完成后,只需使用 . 直接保存数组即可writeToURL...

于 2013-05-06T18:07:15.517 回答