如何使用arrayWithContentsOfFile加载在ObjectiveC/XCode中使用writeToFile成功编写的文件

Jen*_*nny 6 cocoa cocoa-touch objective-c

我正在将一组自定义对象保存到plist列表文件中,如下所示:

+(void) arrayToPlist: (NSArray*) plant_types{
    NSMutableArray* dictionary_array = [NSMutableArray arrayWithCapacity: plant_types.count];

    for(int i = 0; i<plant_types.count; i++){
        PlantType* pt = [plant_types objectAtIndex: i];
        [dictionary_array addObject: [pt toDict]];
    }

    NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString* filePath    = [rootPath stringByAppendingPathComponent:@"PlantTypes.plist"];
    NSLog(@"Writing plist to: %@", filePath);

    [dictionary_array writeToFile: filePath atomically: YES];  
}
Run Code Online (Sandbox Code Playgroud)

这会创建一个看起来像这样的文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
    <dict>
        <key>name</key>
        <string>Autumn Olive</string>
        <key>radius</key>
        <real>10</real>
    </dict>
    <dict>
        <key>name</key>
        <string>Dwarf Cherry</string>
        <key>radius</key>
        <real>5</real>
    </dict>
    <dict>
        <key>name</key>
        <string>Bamboo</string>
        <key>radius</key>
        <real>2</real>
    </dict>
    <dict>
        <key>name</key>
        <string>Pomegranate</string>
        <key>radius</key>
        <real>6</real>
    </dict>
    <dict>
        <key>name</key>
        <string>Lupin</string>
        <key>radius</key>
        <real>0.60000002384185791</real>
    </dict>
</array>
</plist>
Run Code Online (Sandbox Code Playgroud)

并以这种方式加载它们:

+(NSMutableArray*) arrayFromPlist{
    NSLog(@"Loading array of plant types from plist.");

    NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString* filePath    = [rootPath stringByAppendingPathComponent:@"PlantTypes.plist"];
    NSFileManager*  fileManager = [NSFileManager defaultManager];

    if([fileManager fileExistsAtPath:filePath]){ 
        NSLog(@"Plist file exists at expected location.");
        NSMutableArray* plant_dicts = [NSMutableArray arrayWithContentsOfFile:filePath];
        NSLog(@"Loaded array with contents of file, got %d plant types.", plant_dicts.count);
        NSMutableArray* plant_types = [NSMutableArray arrayWithCapacity: plant_dicts.count];
        for(int i = 0; i< plant_dicts.count; i++){
            NSMutableDictionary* dict = [plant_dicts objectAtIndex: i];
            [plant_types addObject: [PlantType fromDict: dict]];
        }
        return plant_types;
    }
    NSLog(@"Plant Type plist file not found.");
    return NULL;
}
Run Code Online (Sandbox Code Playgroud)

没有崩溃,但每次我返回一个NSMutableArray大小为零(并且该日志声明确认plant_dicts大小为零).

我究竟做错了什么?我所看到的所有类似问题arrayWithContentsOfFile:都是人们用手或编辑器创建plist ...我认为从代码本身创建它不会有这些问题......

编辑:我已经确认我不再获得零大小的结果(这很奇怪,自原始帖子以来,该部分中没有代码更改).但是,我仍然没有正确加载东西.这是我的fromDict和toDict Methods.

问题是字符串"shape"似乎没有正确加载.至少,当我问shape == @"Circle"时,我得到它不会,即使它在NSLog语句中显然也是如此.我在代码的其他地方进行相同的比较(当检查如何渲染对象时)并且它在那里工作正常(所以我假设没有像Java中那样奇怪的== vs .equals).

双重编辑:等等,没有[形状isEqualToString:@"Circle"] ....为什么我不需要在使用之前使用它,但在此处添加它有帮助.

Mat*_*err 5

使用plist,此代码返回正确的结果

+(NSMutableArray*) arrayFromPlist{
    NSLog(@"Loading array of plant types from plist.");

    NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString* filePath    = [rootPath stringByAppendingPathComponent:@"PlantTypes.plist"];
    NSFileManager*  fileManager = [NSFileManager defaultManager];

    if([fileManager fileExistsAtPath:filePath]){ 
        NSLog(@"Plist file exists at expected location.");
        NSMutableArray* plant_dicts = [NSMutableArray arrayWithContentsOfFile:filePath];
        NSLog(@"Loaded array with contents of file, got %d plant types.", plant_dicts.count);
        NSMutableArray* plant_types = [NSMutableArray arrayWithCapacity: plant_dicts.count];
        for(int i = 0; i< plant_dicts.count; i++){
            NSMutableDictionary* dict = [plant_dicts objectAtIndex: i];
            [plant_types addObject: dict];
        }
        return plant_types;
    }
    NSLog(@"Plant Type plist file not found.");
    return NULL;
}
Run Code Online (Sandbox Code Playgroud)

结果

2012-02-22 16:24:43.697 Test[5574:10103] Loading array of plant types from plist.
2012-02-22 16:24:43.698 Test[5574:10103] Plist file exists at expected location.
2012-02-22 16:24:43.699 Test[5574:10103] Loaded array with contents of file, got 5 plant types.
2012-02-22 16:24:43.700 Test[5574:10103](
        {
        name = "Autumn Olive";
        radius = 10;
    },
        {
        name = "Dwarf Cherry";
        radius = 5;
    },
        {
        name = Bamboo;
        radius = 2;
    },
        {
        name = Pomegranate;
        radius = 6;
    },
        {
        name = Lupin;
        radius = "0.6000000238418579";
    }
)
Run Code Online (Sandbox Code Playgroud)

我认为问题来自您的PlantType实现.你可以发布你的代码(toDict和fromDict方法)