Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 如何使用arrayWithContentsOfFile加载在Objective/XCode中使用writeToFile成功写入的文件_Objective C_Cocoa Touch_Cocoa - Fatal编程技术网

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

Objective c 如何使用arrayWithContentsOfFile加载在Objective/XCode中使用writeToFile成功写入的文件,objective-c,cocoa-touch,cocoa,Objective C,Cocoa Touch,Cocoa,我正在将自定义对象数组保存到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 = [plan

我正在将自定义对象数组保存到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];  
}
+(void)数组plist:(NSArray*)植物类型{
NSMutableArray*dictionary_array=[NSMutableArray阵列容量:plant_types.count];

对于具有plist的(inti=0;i,此代码返回正确的结果

+(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;
}
我认为问题来自PlantType的实现。您可以发布代码(toDict和fromDict方法)

这一行:

NSMutableDictionary* dict = [plant_dicts objectAtIndex: i];
不返回可变字典,但返回不可变字典

如果您希望它是可变的,您应该按照以下思路做一些事情:

MutableDictionary* dict = [NSMutableDictionary dictionaryWithDictionary: [plant_dicts objectAtIndex: i]];

是的,对于要执行isEqualToString的字符串,原因与Java中相同。==只检查指针是否相同,而不是字符串的内容。该==在渲染代码中起作用,因为编译器知道它可以对同一字符串的两个实例使用同一指针。但是如果其中一个字符串来自plist,它将是一个不同的指针。
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";
    }
)
NSMutableDictionary* dict = [plant_dicts objectAtIndex: i];
MutableDictionary* dict = [NSMutableDictionary dictionaryWithDictionary: [plant_dicts objectAtIndex: i]];