gpt4 book ai didi

objective-c - 如何使用 arrayWithContentsOfFile 加载在 ObjectiveC/XCode 中用 writeToFile 成功写入的文件

转载 作者:太空狗 更新时间:2023-10-30 03:44:15 26 4
gpt4 key购买 nike

我正在将一组自定义对象保存到 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, 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;
}

没有崩溃,但每次我返回一个大小为零的 NSMutableArray(并且日志语句确认 plant_dicts 的大小为零)。

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

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

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

双重编辑:等等,没有 [shape isEqualToString: @"Circle"]...为什么我之前不需要使用它让我感到困惑,但在这里添加它会有所帮助。

最佳答案

使用您的 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;
}

结果

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 成功写入的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9343103/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com