简体   繁体   English

将NSMutableArray保存到NSUserDefaults的最佳方法是什么?

[英]What is the best way to save an NSMutableArray to NSUserDefaults?

I have a custom object called Occasion defined as follows: 我有一个名为Occasion的自定义对象,定义如下:

#import <Foundation/Foundation.h>


@interface Occasion : NSObject {

NSString *_title;
NSDate *_date;
NSString *_imagePath;    

}

@property (nonatomic, retain) NSString *title;
@property (nonatomic, retain) NSDate *date;
@property (nonatomic, retain) NSString *imagePath;

Now I have an NSMutableArray of Occasions which I want to save to NSUserDefaults. 现在我有一个NSMutableArray of Occasions,我想保存到NSUserDefaults。 I know it's not possible in a straight forward fashion so I'm wondering which is the easiest way to do that? 我知道这是不可能的,所以我想知道哪种方法最简单? If serialization is the answer, then how? 如果序列化是答案,那么如何? Because I read the docs but couldn't understand the way it works fully. 因为我阅读了文档但却无法理解它的完整工作方式。

You should use something like NSKeyedArchiver to serialize the array to an NSData , save it to the NSUserDefaults and then use NSKeyedUnarchiver to deserialize it later: 您应该使用NSKeyedArchiver的序列将数组序列化为NSData ,将其保存到NSUserDefaults ,然后使用NSKeyedUnarchiver将其反序列化:

NSData *serialized = [NSKeyedArchiver archivedDataWithRootObject:myArray];
[[NSUserDefaults standardUserDefaults] setObject:serialized forKey:@"myKey"];

//...

NSData *serialized = [[NSUserDefaults standardUserDefaults] objectForKey:@"myKey"];
NSArray *myArray = [NSKeyedUnarchiver unarchiveObjectWithData:serialized];

You will need to implement the NSCoding protocol in your Occasion class and correctly save the various properties to make this work correctly. 您需要在Occasion类中实现NSCoding协议,并正确保存各种属性以使其正常工作。 For more information see the Archives and Serializations Programming Guide . 有关更多信息,请参阅“ 存档和序列化编程指南” It shouldn't be more than a few lines of code to do this. 执行此操作不应超过几行代码。 Something like: 就像是:

- (void)encodeWithCoder:(NSCoder *)coder {
    [super encodeWithCoder:coder];

    [coder encodeObject:_title forKey:@"_title"];
    [coder encodeObject:_date forKey:@"_date"];
    [coder encodeObject:_imagePath forKey:@"_imagePath"];
}

- (id)initWithCoder:(NSCoder *)coder {
    self = [super initWithCoder:coder];

    _title = [[coder decodeObjectForKey:@"_title"] retain];
    _date = [[coder decodeObjectForKey:@"_date"] retain];
    _imagePath = [[coder decodeObjectForKey:@"_imagePath"] retain];

    return self;
}

NSUserDefaults is intended for user preferences, not storing application data. NSUserDefaults用于用户首选项,而不是存储应用程序数据。 Use CoreData or serialize the objects into the documents directory. 使用CoreData或将对象序列化到文档目录中。 You'll need to have your class implement the NSCoding protocol for it to work. 你需要让你的类实现NSCoding协议才能工作。

1) Implement NSCoding in Occasion.h 1)在Occasion.h实现NSCoding

@interface Occasion : NSObject <NSCoding>

2) Implement the protocol in Occasion.m 2)在Occasion.m实现协议

- (id)initWithCoder:(NSCoder *)aDecoder {

    if (self = [super init]) {

        self.title = [aDecoder decodeObjectForKey:@"title"];
        self.date = [aDecoder decodeObjectForKey:@"date"];
        self.imagePath = [aDecoder decodeObjectForKey:@"imagePath"];

    }            
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder {

    [aCoder encodeObject:title forKey:@"title"];
    [aCoder encodeObject:date forKey:@"date"];
    [aCoder encodeObject:imagePath forKey:@"imagePath"];
}

3) Archive the data to a file in documents directory 3)将数据存档到文档目录中的文件

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                    NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *path= [documentsPath stringByAppendingPathComponent:@“occasions”];
[NSKeyedArchiver archiveRootObject:occasions toFile:path];

4) To unarchive... 4)取消归档......

NSMutableArray *occasions = [NSKeyedUnarchiver unarchiveObjectWithFile:path];

You could implement NSCoding in Occasion . 您可以在Occasion实施NSCoding

You then use [NSKeyedArchiver archivedDataWithRootObject:myArray] to create an NSData object from the array. 然后使用[NSKeyedArchiver archivedDataWithRootObject:myArray]从数组创建NSData对象。 You can put this into user defaults. 您可以将其置于用户默认值中。

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

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