gpt4 book ai didi

iphone - 在 NSMutableDictionary 的派生类中未调用encodeWithCoder

转载 作者:行者123 更新时间:2023-12-03 20:43:06 26 4
gpt4 key购买 nike

基本上我使用的是一些名为 OrderedDictionary 的开源代码,它派生自 NSMutableDictionary。然后我想通过向 OrderedDictionary 类添加编码和解码方法来将有序字典数据保存到 NSUserDefaults 中。但是,我意识到编码和解码方法没有被调用,因此,解码的字典不再排序。下面是我的代码:

@interface OrderedDictionary : NSMutableDictionary <NSCopying, NSCoding>
{
NSMutableDictionary *dictionary;
NSMutableArray *array;
}

在实现文件中:

/**
* encode the object
**/
- (void)encodeWithCoder:(NSCoder *)coder
{
[super encodeWithCoder:coder];
[coder encodeObject:dictionary forKey:@"dictionary"];
[coder encodeObject:array forKey:@"array"];
}

/**
* decode the object
*/
- (id)initWithCoder:(NSCoder *)coder
{
self = [super initWithCoder:coder];
if (self != nil)
{
dictionary = [coder decodeObjectForKey:@"dictionary"];
array = [coder decodeObjectForKey:@"array"];
}
return self;
}

使用此功能的快速示例代码:

dictionary = [[OrderedDictionary alloc] init];
[dictionary setObject:@"one" forKey:@"two"];
[dictionary setObject:@"what" forKey:@"what"];
[dictionary setObject:@"7" forKey:@"7"];
NSLog(@"Final contents of the dictionary: %@", dictionary);

if ([[NSUserDefaults standardUserDefaults] objectForKey:@"myDictionary"] == nil)
{
[[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:dictionary]
forKey:@"myDictionary"];
}
else
{
NSUserDefaults *currentDefaults = [NSUserDefaults standardUserDefaults];

NSData *savedDictionary = [currentDefaults objectForKey:@"myDictionary"];

if (savedDictionary != nil)
{
OrderedDictionary *oldData = [NSKeyedUnarchiver unarchiveObjectWithData:savedDictionary];
if (oldData != nil)
{
NSLog(@"Final contents of the retrieved: %@", oldData);

}
}
}

问题是,最终的 returnedDictionary 没有原始数据顺序,并且根本不调用编码和解码方法。

感谢您提前提供的任何帮助! :)

最佳答案

有一个名为 -classForCoder 的 NSObject 方法,您需要在 OrderedDictionary 中重写该方法。来自文档:

classForCoder
Overridden by subclasses to substitute a class other than its own during coding.

-(Class)classForCoder
Return Value
The class to substitute for the receiver's own class during coding.

Discussion
This method is invoked by NSCoder. NSObject’s implementation returns the receiver’s class. The private subclasses of a class cluster substitute the name of their public superclass when being archived.

最后一行是关键。因此,在 OrderedDictionary.m 中:

- (Class)classForCoder 
{
return [self class]; // Instead of NSMutableDictionary
}

此外,如果您不使用 ARC,请确保保留从 -decodeObjectForKey 返回的对象。正如 rob mayoff 在下面提到的,您不应该调用 [super initWithCoder:] (或 [superencodeWithCoder:')。我还更改了键字符串以避免冲突。

- (id)initWithCoder:(NSCoder *)coder
{
if (self != nil)
{
dictionary = [[coder decodeObjectForKey:@"OrderedDictionary_dictionary"] retain];
array = [[coder decodeObjectForKey:@"OrderedDictionary_array"] retain];
}
return self;
}

关于iphone - 在 NSMutableDictionary 的派生类中未调用encodeWithCoder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10004783/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com