ios存储自定义对象

时间:2013-03-27 17:29:11

标签: iphone ios smartphone

如何存储NSMutableArray个自定义对象? 我有这个用于加载和保存文件的代码:

- (NSMutableArray *)loadDataFromFile:(NSString *)fileName {
    NSError *error;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docDir = [paths objectAtIndex:0];
    NSString *path = [docDir stringByAppendingPathComponent:fileName];
    NSFileManager *fileMgr = [NSFileManager defaultManager];
    if (![fileMgr fileExistsAtPath:path]) {
        NSArray *fileArray = [fileName componentsSeparatedByString:@"."];
        NSString *name = [fileArray objectAtIndex:0];
        NSString *ext = [fileArray objectAtIndex:1];
        NSString *bundle = [[NSBundle mainBundle] pathForResource:name ofType:ext];
        [fileMgr copyItemAtPath:bundle toPath:path error:&error];
    }
    NSMutableArray *data = [[NSMutableArray alloc] initWithContentsOfFile:path];
    return data;
}

- (void)saveData:(NSMutableArray *)arrayData toFile:(NSString *)filename forKey:(NSString *)key {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docDir = [paths objectAtIndex:0];
    NSString *path = [docDir stringByAppendingPathComponent:filename];
    [arrayData writeToFile: path atomically:YES];
    NSLog(@"%@", arrayData);
}

但是当我使用data.plist作为文件名时,它不起作用,因为NSLog(@"%@", arrayData);返回列表自定义对象地址:

  

“AreaTableRecord:0x76a7ef0”

使用以下代码将此自定义对象插入到数组中:

AreaTableRecord *area=[[AreaTableRecord alloc] init];
        area.title=title;
        area.lastScore=0;
        area.vocabulary=[[NSMutableArray alloc] init];
        [self.areas addObject:area];

如何存储包含自定义对象AreaTableRecord的NSMutableArray self.areas? 和 我用什么文件格式来存储这些数据? (在我看来,plist在这种情况下不起作用)

1 个答案:

答案 0 :(得分:1)

您只能在NSDefaults或plist中存储原始数据类型。为了解决这个问题,您可以选择将信息存储在数据库中....或者将对象编码为字节流,然后将它们保存到文件中。

看看这个thread。它详细介绍了如何对对象进行编码。

基本上,您需要将这些方法添加到自定义类中:

- (void)encodeWithCoder:(NSCoder *)encoder
{
       //Encode properties, other class variables, etc
    [encoder encodeObject:self.question forKey:@\"question\"];
    [encoder encodeObject:self.categoryName forKey:@\"category\"];
    [encoder encodeObject:self.subCategoryName forKey:@\"subcategory\"];
}


- (id)initWithCoder:(NSCoder *)decoder
{
    self = [super init];
    if( self != nil )
    {
               //decode properties, other class vars
        self.question = [decoder decodeObjectForKey:@\"question\"];
        self.categoryName = [decoder decodeObjectForKey:@\"category\"];
        self.subCategoryName = [decoder decodeObjectForKey:@\"subcategory\"];
    }
    return self;
}

然后为了使用它们,你可以这样打电话:

用于设置:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *myEncodedObject = [NSKeyedArchiver archivedDataWithRootObject:obj];  
[defaults setObject:myEncodedObject forKey:@\"myEncodedObjectKey\"];

用于检索:

NSUserDefaults defaults = [NSUserDefaults standardUserDefaults];
NSData *myEncodedObject = [defaults objectForKey: key];
MyCustomObject* obj = (MyCustomObject*)[NSKeyedUnarchiver unarchiveObjectWithData: myEncodedObject];