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

发布于 2024-12-09 05:54:08 字数 475 浏览 0 评论 0原文

我有一个名为 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;

现在我有一个 NSMutableArray of Occasions,我想将其保存到 NSUserDefaults 中。我知道这是不可能的,所以我想知道哪种方法最简单?如果序列化是答案,那么如何实现呢?因为我阅读了文档,但无法完全理解它的工作方式。

I have a custom object called Occasion defined as follows:

#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. 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

叫思念不要吵 2024-12-16 05:54:08

您应该使用类似 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];

您需要在 Occasion 类中实现 NSCoding 协议,并正确保存各种属性才能使其正常工作。有关详细信息,请参阅存档和序列化编程指南。执行此操作不应超过几行代码。像这样的东西:

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

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:

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. 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;
}
勿挽旧人 2024-12-16 05:54:08

NSUserDefaults 用于用户首选项,而不是存储应用程序数据。使用 CoreData 或序列化对象进入文档目录。您需要让您的类实现 NSCoding 协议才能正常工作。

1) 在 Occasion.h 中实现 NSCoding

@interface Occasion : NSObject <NSCoding>

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) 将数据归档到文档目录中的文件

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

4 ) 取消存档...

NSMutableArray *occasions = [NSKeyedUnarchiver unarchiveObjectWithFile:path];

NSUserDefaults is intended for user preferences, not storing application data. Use CoreData or serialize the objects into the documents directory. You'll need to have your class implement the NSCoding protocol for it to work.

1) Implement NSCoding in Occasion.h

@interface Occasion : NSObject <NSCoding>

2) Implement the protocol in 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

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

4) To unarchive...

NSMutableArray *occasions = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
孤独难免 2024-12-16 05:54:08

您可以在Occasion中实现NSCoding

然后,您使用 [NSKeyedArchiver archivedDataWithRootObject:myArray] 从数组创建一个 NSData 对象。您可以将其放入用户默认值中。

You could implement NSCoding in Occasion.

You then use [NSKeyedArchiver archivedDataWithRootObject:myArray] to create an NSData object from the array. You can put this into user defaults.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文