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.
我有一个实现NSCoding的Objective-C类。我需要以NSDictionary的形式返回该类实例的表示形式,其中键是属性名,值是属性值。
How do I make this conversion in Objective-C ?
如何在Objective-C中进行转换?
3 个解决方案
#1
3
It should be straightforward to create an NSCoder
subclass, whose encode<thing>:forKey:
methods just stick the thing
s into a dictionary, which you can then retrieve.
创建一个NSCoder子类应该很简单,它的编码为
@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
Then you create an instance of your coder, and send encodeWithCoder:
to this object you want in a dictionary.
然后创建编码器的一个实例,并将encodeWithCoder发送到字典中需要的这个对象。
#2
8
NSObject has a method dictionaryWithValuesForKeys:
. From the documentation:
NSObject有一个method dictionaryWithValuesForKeys:。从文档:
Returns a dictionary containing the property values identified by each of the keys in a given array.
返回包含给定数组中每个键标识的属性值的字典。
There's also a corresponding -setValuesForKeysWithDictionary:
method.
还有一个相应的-setValuesForKeysWithDictionary:方法。
NSCoding doesn't actually come in to play here.
NSCoding不是来这里玩的。
#3
0
What Andrew Madsen said. If you wanted to use NSCoding though, you would've to encode your object to an NSData object using NSKeyedArchiver an put that NSData in your dictionary. Wouldn't be human-readable though, if that is of concern. Cheers
安德鲁·马德森说。如果你想要使用NSCoding,你必须使用NSKeyedArchiver将你的对象编码到NSData对象中把NSData放到你的字典中。如果这是一个问题的话,那就不是人类可读的了。干杯
Edit: re-reading your question, it's really what Andrew said since you want 1:1 representation of key:values in a dictionary - the NSCoding approach would give you the whole object as value for your dictionary key. So, +1 to Andrew.
编辑:重读你的问题,这正是Andrew所说的,因为你想要一个键的1:1表示:一个字典中的值——NSCoding方法会给你整个对象作为你的字典键的值。所以,安德鲁+ 1。
#1
3
It should be straightforward to create an NSCoder
subclass, whose encode<thing>:forKey:
methods just stick the thing
s into a dictionary, which you can then retrieve.
创建一个NSCoder子类应该很简单,它的编码为
@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
Then you create an instance of your coder, and send encodeWithCoder:
to this object you want in a dictionary.
然后创建编码器的一个实例,并将encodeWithCoder发送到字典中需要的这个对象。
#2
8
NSObject has a method dictionaryWithValuesForKeys:
. From the documentation:
NSObject有一个method dictionaryWithValuesForKeys:。从文档:
Returns a dictionary containing the property values identified by each of the keys in a given array.
返回包含给定数组中每个键标识的属性值的字典。
There's also a corresponding -setValuesForKeysWithDictionary:
method.
还有一个相应的-setValuesForKeysWithDictionary:方法。
NSCoding doesn't actually come in to play here.
NSCoding不是来这里玩的。
#3
0
What Andrew Madsen said. If you wanted to use NSCoding though, you would've to encode your object to an NSData object using NSKeyedArchiver an put that NSData in your dictionary. Wouldn't be human-readable though, if that is of concern. Cheers
安德鲁·马德森说。如果你想要使用NSCoding,你必须使用NSKeyedArchiver将你的对象编码到NSData对象中把NSData放到你的字典中。如果这是一个问题的话,那就不是人类可读的了。干杯
Edit: re-reading your question, it's really what Andrew said since you want 1:1 representation of key:values in a dictionary - the NSCoding approach would give you the whole object as value for your dictionary key. So, +1 to Andrew.
编辑:重读你的问题,这正是Andrew所说的,因为你想要一个键的1:1表示:一个字典中的值——NSCoding方法会给你整个对象作为你的字典键的值。所以,安德鲁+ 1。