本文介绍了如何利用NSCoding将对象转换为NSDictionary?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实现NSCoding的Objective-C类。我需要以NSDictionary的形式返回这个类的实例的表示,其中键是属性名称,值是属性值。

I have an Objective-C class that implements NSCoding. I need to return a representation of an instance of this class in the form of an NSDictionary, where the keys are the property names and the values are the property values.

如何我在Objective-C中进行这种转换?

How do I make this conversion in Objective-C ?

推荐答案

应该直接创建子类,其 encode< thing>:forKey:方法只需将东西 s放入字典,然后可以检索。

It should be straightforward to create an NSCoder subclass, whose encode<thing>:forKey: methods just stick the things into a dictionary, which you can then retrieve.

@implementation DriisDictCoder
{
    NSMutableDictionary * encodedDict;
}

- (void)encodeDouble: (double)v forKey: (NSString *)k {
    [codedDict addObject:[NSNumber numberWithDouble:v] forKey:k];
}

// etc.

// Custom method
- (NSDictionary *)dictForEncodedObject {
     return encodedDict;
}

@end

您的编码器,并将 encodeWithCoder:发送到您希望在字典中的此对象。

Then you create an instance of your coder, and send encodeWithCoder: to this object you want in a dictionary.

这篇关于如何利用NSCoding将对象转换为NSDictionary?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 09:02