Objective C:我试图保存一个包含基元和对象的 NSDictionary,但是当我尝试添加自定义对象时发生错误
所以我有一个名为“Box”的自定义对象,它抛出了该对象的错误。 我的 NSDictionary 内部是...
box = [[Box alloc] initWithHeight:20 height:40];
wrap.dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:numberInt, kInt, numberShort, kShort, numberLong, kLong, numberFloat, kFloat, numberDouble, kDouble, numberBool, kBool, nsString, kString, nsDate, kDate, box, @"keybox", nil];
注意我在顶部创建并添加到 NSDictionary 最后的盒子对象。之前当盒子不在那里时,一切都工作正常,但是当我添加自定义对象“盒子”时,它无法再保存。
- (void) saveDataToDisk:(NSMutableDictionary *)dictionaryForDisk
{
NSMutableData *data = [NSMutableData data];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:data];
//Encode
[archiver setOutputFormat:NSPropertyListBinaryFormat_v1_0];
[archiver encodeObject:dictionaryForDisk forKey:@"Dictionary_Key"];
[archiver finishEncoding];
[data writeToFile:[self pathForDataFile] atomically:YES];
}
- (NSString *) pathForDataFile {
NSArray* documentDir = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString* path = nil;
if (documentDir)
{
path = [documentDir objectAtIndex:0];
}
return [NSString stringWithFormat:@"%@/%@", path, @"data.bin"];
}
这是错误
-[Box encodeWithCoder:]: unrecognized selector sent to instance 0x4e30d60
2011-11-11 11:56:43.430 nike[1186:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Box encodeWithCoder:]: unrecognized selector sent to instance 0x4e30d60'
*** Call stack at first throw:
任何帮助将不胜感激!!!!
So I have this custom object i made called 'Box', it's throwing the error for that one.
Inside of my NSDictionary is...
box = [[Box alloc] initWithHeight:20 height:40];
wrap.dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:numberInt, kInt, numberShort, kShort, numberLong, kLong, numberFloat, kFloat, numberDouble, kDouble, numberBool, kBool, nsString, kString, nsDate, kDate, box, @"keybox", nil];
Notice the box object that I created up top and added in the NSDictionary at the very end. Before when the box wasn't in there, everything worked fine, but when I added the custom object 'Box', it couldn't save anymore.
- (void) saveDataToDisk:(NSMutableDictionary *)dictionaryForDisk
{
NSMutableData *data = [NSMutableData data];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:data];
//Encode
[archiver setOutputFormat:NSPropertyListBinaryFormat_v1_0];
[archiver encodeObject:dictionaryForDisk forKey:@"Dictionary_Key"];
[archiver finishEncoding];
[data writeToFile:[self pathForDataFile] atomically:YES];
}
- (NSString *) pathForDataFile {
NSArray* documentDir = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString* path = nil;
if (documentDir)
{
path = [documentDir objectAtIndex:0];
}
return [NSString stringWithFormat:@"%@/%@", path, @"data.bin"];
}
This was the error
-[Box encodeWithCoder:]: unrecognized selector sent to instance 0x4e30d60
2011-11-11 11:56:43.430 nike[1186:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Box encodeWithCoder:]: unrecognized selector sent to instance 0x4e30d60'
*** Call stack at first throw:
Any help would be greatly appreciated!!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该异常告诉您它正在尝试将
-encodeWithCoder:
发送到您的Box
实例。如果您查找 文档,可以看到这个属于NSCoding
协议。NSCoding
由 NSKeyedArchiver 用于对对象进行编码。NSDictionary
的NSCoding
实现要求其内部的所有键和对象也符合NSCoding
。在您的情况下,存储在字典中的所有先前值都符合NSCoding
,但您的Box
实例不符合。您需要在您的
Box
类上实现-initWithCoder:
和-encodeWithCoder:
,然后声明您的类符合NSCoding 通过将声明修改为如下所示
The exception tells you that it's trying to send
-encodeWithCoder:
to yourBox
instance. If you look up the documentation, you can see that this belongs to theNSCoding
protocol.NSCoding
is used byNSKeyedArchiver
to encode objects.NSDictionary
's implementation ofNSCoding
requires that all keys and objects inside it also conform toNSCoding
. In your case, all the previous values stored in your dictionary conform toNSCoding
, but yourBox
instance doesn't.You need to implement
-initWithCoder:
and-encodeWithCoder:
on yourBox
class and then declare that your class conforms toNSCoding
by modifying the declaration to look like您的自定义 Box 类必须遵守
NSCoding
协议。协议文档位于此处< /a>. Ray Wenderlich 有一个很好的教程这里。Your custom Box class must adhere to the
NSCoding
Protocol. The protocol documentation is here. Ray Wenderlich has a good tutorial here.