objective-c – 如何使用arrayWithContentsOfFile加载在ObjectiveCXCode中使用writeToFile成功编写的文件

objective-c – 如何使用arrayWithContentsOfFile加载在ObjectiveCXCode中使用writeToFile成功编写的文件,第1张

概述我正在将一组自定义对象保存到plist列表文件中,如下所示: +(void) arrayToPlist: (NSArray*) plant_types{ NSMutableArray* dictionary_array = [NSMutableArray arrayWithCapacity: plant_types.count]; for(int i = 0; i<plant_t 我正在将一组自定义对象保存到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];  }

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

<?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,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的大小为零).

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

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

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

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

解决方法 使用pList,此代码返回正确的结果

+(NSMutableArray*) arrayFromPList{    NSLog(@"Loading array of plant types from pList.");    Nsstring *rootPath = [NSSearchPathForDirectorIEsInDomains(NSdocumentDirectory,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方法)

总结

以上是内存溢出为你收集整理的objective-c – 如何使用arrayWithContentsOfFile加载在ObjectiveC/XCode中使用writeToFile成功编写的文件全部内容,希望文章能够帮你解决objective-c – 如何使用arrayWithContentsOfFile加载在ObjectiveC/XCode中使用writeToFile成功编写的文件所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1017287.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存