1

如何存储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);返回列表自定义对象地址:

“区域表记录:0x76a7ef0”

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

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

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

4

1 回答 1

1

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

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

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

- (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];
于 2013-03-27T17:36:03.763 回答