简体   繁体   English

ios 存储自定义对象

[英]ios storing custom objects

How can I store NSMutableArray of custom objects?如何存储自定义对象的NSMutableArray I have this code for loading and saving files:我有用于加载和保存文件的代码:

- (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);
}

But when I used data.plist as filename, it didn't work because NSLog(@"%@", arrayData);但是当我使用data.plist作为文件名时,它不起作用,因为NSLog(@"%@", arrayData); returns list custom object adresses:返回列表自定义对象地址:

"AreaTableRecord: 0x76a7ef0" “区域表记录:0x76a7ef0”

This custom object is inserted to array using this code:使用以下代码将此自定义对象插入到数组中:

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

How could I store NSMutableArray self.areas that contains custom objects AreaTableRecord?我怎么能存储NSMutableArray self.areas包含自定义对象AreaTableRecord? and What file format shloud I use to store this data?以及我使用什么文件格式来存储这些数据? (it seems to me that plist is not working in this case) (在我看来 plist 在这种情况下不起作用)

You are only able to store primitive data types in NSDefaults or a plist.您只能在 NSDefaults 或 plist 中存储原始数据类型。 In order to work around this you can either choose to store your information in a database....or encode your objects as byte streams and then save them into a file.为了解决这个问题,您可以选择将您的信息存储在数据库中……或者将您的对象编码为字节流,然后将它们保存到文件中。

Take a look at this thread .看看这个线程 It details how to go about encoding your objects.它详细说明了如何对对象进行编码。

Basically you need to add these methods to your custom class:基本上,您需要将这些方法添加到您的自定义类中:

- (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;
}

And then in order to use them you make calls as such:然后为了使用它们,你可以这样调用:

For setting:对于设置:

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

For retrieving:用于检索:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM