在 NSMutableDictionary 中存储自定义对象

发布于 2024-10-31 09:14:35 字数 789 浏览 1 评论 0 原文

我正在尝试在 NSMutableDictionary 中存储自定义对象。保存后,当我从 NSMutableDictionary 读取对象时,它始终为空。

这是代码

//Saving

NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];

CustomObject *obj1 = [[CustomObject alloc] init];
obj1.property1 = @"My First Property";

[dict setObject:obj1 forKey:@"FirstObjectKey"];
[dict writeToFile:[self dataFilePath] atomically:YES];

// Reading

 NSString *filePath = [self dataFilePath];
        NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];

        CustomObject *tempObj = [dict objectForKey:@"FirstObjectKey"];

        NSLog(@"Object %@", tempObj);
        NSLog(@"property1:%@,,tempObj.property1);

如何在 NSMutableDictionary 中存储自定义类对象?

I am trying to store a custom object in NSMutableDictionary. After saving when I read the object from NSMutableDictionary it's always null.

Here is the code

//Saving

NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];

CustomObject *obj1 = [[CustomObject alloc] init];
obj1.property1 = @"My First Property";

[dict setObject:obj1 forKey:@"FirstObjectKey"];
[dict writeToFile:[self dataFilePath] atomically:YES];

// Reading

 NSString *filePath = [self dataFilePath];
        NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];

        CustomObject *tempObj = [dict objectForKey:@"FirstObjectKey"];

        NSLog(@"Object %@", tempObj);
        NSLog(@"property1:%@,,tempObj.property1);

How can I store a custom class object in NSMutableDictionary?

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

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

发布评论

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

评论(2

愁杀 2024-11-07 09:14:35

问题不在于将对象放入字典中;而在于将对象放入字典中。问题在于将其写入文件。

您的自定义类必须是 可序列化。您需要实现 < code>NSCoding 协议,以便当您要求将类写入磁盘时,Cocoa 知道如何处理您的类。

这很简单;您需要实现两种如下所示的方法:

- (id)initWithCoder:(NSCoder *)coder {
    self = [super init];
    // If inheriting from a class that implements initWithCoder:
    // self = [super initWithCoder:coder];
    myFirstIvar = [[coder decodeObjectForKey:@"myFirstIvar] retain];
    mySecondIvar = [[coder decodeObjectForKey:@"mySecondIvar] retain];
    // etc.

    return self;
}

- (void)encodeWithCoder:(NSCoder *)coder {
    // If inheriting from a class that implements encodeWithCoder:
    // [super encodeWithCoder:coder];
    [coder encodeObject:myFirstIvar forKey:@"myFirstIvar"];
    [coder encodeObject:mySecondIvar forKey:@"mySecondIvar"];
    // etc.
}

本质上您只是列出需要保存的 ivars,然后正确地读回它们。

更新:正如 Eimantas 提到的,您还需要 NSKeyedArchiver。保存:

NSData * myData = [NSKeyedArchiver archivedDataWithRootObject:myDict];
BOOL result = [myData writeToFile:[self dataFilePath] atomically:YES];

重新加载:

NSData * myData = [NSData dataWithContentsOfFile:[self dataFilePath]];
NSDictionary * myDict = [NSKeyedUnarchiver unarchiveObjectWithData:myData];

我认为应该这样做。

The problem is not with putting the object into the dictionary; the problem is with writing it to a file.

Your custom class has to be serializable. You need to implement the NSCoding protocol so that Cocoa knows what to do with your class when you ask for it to be written out to disk.

This is pretty simple to do; you need to implement two methods that will look something like the following:

- (id)initWithCoder:(NSCoder *)coder {
    self = [super init];
    // If inheriting from a class that implements initWithCoder:
    // self = [super initWithCoder:coder];
    myFirstIvar = [[coder decodeObjectForKey:@"myFirstIvar] retain];
    mySecondIvar = [[coder decodeObjectForKey:@"mySecondIvar] retain];
    // etc.

    return self;
}

- (void)encodeWithCoder:(NSCoder *)coder {
    // If inheriting from a class that implements encodeWithCoder:
    // [super encodeWithCoder:coder];
    [coder encodeObject:myFirstIvar forKey:@"myFirstIvar"];
    [coder encodeObject:mySecondIvar forKey:@"mySecondIvar"];
    // etc.
}

Essentially you're just listing the ivars that you need to save, and then reading them back in properly.

UPDATE: As mentioned by Eimantas, you'll also need NSKeyedArchiver. To save:

NSData * myData = [NSKeyedArchiver archivedDataWithRootObject:myDict];
BOOL result = [myData writeToFile:[self dataFilePath] atomically:YES];

To reload:

NSData * myData = [NSData dataWithContentsOfFile:[self dataFilePath]];
NSDictionary * myDict = [NSKeyedUnarchiver unarchiveObjectWithData:myData];

I think that should do it.

多彩岁月 2024-11-07 09:14:35

writeToFile 方法只能将标准类型的对象存储到 plist 中。如果您有自定义对象,则必须使用 NSKeyedArchiver/NSKeyedUnarchiver 来实现此目的。

writeToFile method can store only standard types of objects into plist. If you have custom object you'd have to use NSKeyedArchiver/NSKeyedUnarchiver for this.

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