Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.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 使用NSMutableDictionary保存数据_Objective C_Nsmutabledictionary - Fatal编程技术网

Objective c 使用NSMutableDictionary保存数据

Objective c 使用NSMutableDictionary保存数据,objective-c,nsmutabledictionary,Objective C,Nsmutabledictionary,我有一种将dic保存到磁盘的方法: +(BOOL) writeApplicationData:(NSDictionary *)data bwriteFileName:(NSString *)fileName { NSLog(@"writeApplicationData"); NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMas

我有一种将dic保存到磁盘的方法:

+(BOOL) writeApplicationData:(NSDictionary *)data 
              bwriteFileName:(NSString *)fileName
{
    NSLog(@"writeApplicationData");
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    if (!documentsDirectory) {
        NSLog(@"Documents directory not found!");
        return NO;
    }
    NSString *appFile = [documentsDirectory stringByAppendingPathComponent:fileName];
    return ([data writeToFile:appFile atomically:YES]);
}
我用以下方法进行了测试:

        NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
        NSMutableDictionary *d1 = [[NSMutableDictionary alloc] init];
        NSMutableDictionary *d2 = [[NSMutableDictionary alloc] init];

        [d1 setObject:@"d11"
               forKey:@"d11"];
        [d1 setObject:@"d12"
               forKey:@"d12"];
        [d1 setObject:@"d13"
               forKey:@"d13"];

        [d2 setObject:@"d21"
               forKey:@"d21"];
        [d2 setObject:@"d22"
               forKey:@"d22"];
        [d2 setObject:@"d23"
               forKey:@"d23"];

        [dic setObject:d1
                forKey:@"d1"];
        [dic setObject:d2
                forKey:@"d2"];

        [self writeApplicationData:dic
                    bwriteFileName:@"testSave"];
数据保存正确

然后我尝试用obj类保存d1:

LevelInfoData *levelInfoData = [[LevelInfoData alloc] init];

[levelInfoDictionary setObject:levelInfoData
            forKey:@"test"];

[dic setObject:levelInfoDictionary
        forKey:@"LevelInfoDictionary"];
但这一次,磁盘中甚至没有生成plist文件

以下是LevelInfoData类:

@interface LevelInfoData : NSObject {

    int levelNum;
}

@property (nonatomic) int levelNum;

@end

@implementation LevelInfoData
@synthesize levelNum;
@synthesize isLevelLocked;
@synthesize isLevelCleared;
@synthesize levelHighScore;

-(id)init
{  
    if( (self = [super init]) ) {

        levelNum = 0;
    }

    return self;
}

@end

我真的很困惑,希望有人能帮我,谢谢。

字典的内容必须是属性列表类型的对象

从NSDictionary类参考:

此方法在写入文件之前递归验证所有包含的对象是否都是NSData、NSDate、NSNumber、NSString、NSArray或NSDictionary的属性列表对象实例,如果所有对象都不是属性列表对象,则返回否,因为生成的文件将不是有效的属性列表


您可能希望尝试将自定义类设置为NSData的子类,而不是NSObject。

字典的内容必须是属性列表类型的对象

从NSDictionary类参考:

此方法在写入文件之前递归验证所有包含的对象是否都是NSData、NSDate、NSNumber、NSString、NSArray或NSDictionary的属性列表对象实例,如果所有对象都不是属性列表对象,则返回否,因为生成的文件将不是有效的属性列表


您可能希望尝试将自定义类设置为NSData的子类,而不是NSObject。

我不确定您对NSDictionary的附加程度,但在这种情况下,NSCoder可能会更好地为您服务

详情如下:


我不确定您对NSDictionary有多感兴趣,但在这种情况下,NSCoder可能会更好地为您服务

详情如下: