如何使用arrayWithContentsOfFile加载文件成功写入writeToFile在ObjectiveC / XCode [英] How to use arrayWithContentsOfFile to load file successfully written with writeToFile in ObjectiveC/XCode

查看:216
本文介绍了如何使用arrayWithContentsOfFile加载文件成功写入writeToFile在ObjectiveC / XCode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am saving an array of custom objects to a plist list file like so:

+(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];  
}

这会创建一个类似以下文件的文件:

Which creates a file that looks like:

<?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>

并以这种方式加载:

+(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;
}

Nothings崩溃,但每次返回 NSMutableArray 大小为零(并且记录语句确认 plant_dicts 大小为零)。

Nothings crashing, but every time I return a NSMutableArray of size zero (and that logging statement confirms that plant_dicts is of size zero).

我做错了什么?所有类似的问题,我看到与 arrayWithContentsOfFile:是人们用手工或编辑器创建plist ...我想从代码本身maken' t有这些问题...

What am I doing wrong? All the similar problems I'm seeing with arrayWithContentsOfFile: is people creating the plist by hand or with an editor...I'd think making it from the code itself wouldn't have these problems...

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

I have confirmed that I'm no longer getting a size zero result (which is weird, there have been no code changes in that section since the original post). However, I'm still not loading things correctly. Here is my fromDict and toDict Methods.

问题是字符串shape似乎没有加载。至少,当我问如果形状== @圆我得到它不,即使它清楚地在NSLog语句中。我在代码的其他地方(当检查如何渲染对象时)进行相同的比较,它在那里工作正常(所以我认为没有一些奇怪的== VS。等于像在Java中)。

The problem is that the string "shape" doesn't seem to be loading right. At least, when I ask if shape == @"Circle" I get that it doesn't, even if it clearly does in the NSLog statement. I am making an identical comparison elsewhere in the code (when checking how to render the object) and it works fine there (so I assume there isn't some weird == vs .equals like there is in Java).

双编辑:等等,没有[shape isEqualToString:@Circle] ....为什么我不需要使用它,

Double Wait, no there is [shape isEqualToString: @"Circle"]....why I didn't need to use it before confuses me, but adding it here helps.

推荐答案

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

With your plist, this code return a correct result

+(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;
}

结果

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";
    }
)

来自您的PlantType实现。您可以发布您的代码(toDict和fromDict方法)

I think problem come from your PlantType implementation. Can you post your code (toDict and fromDict methods)

这篇关于如何使用arrayWithContentsOfFile加载文件成功写入writeToFile在ObjectiveC / XCode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆