Objective C:我试图保存一个包含基元和对象的 NSDictionary,但是当我尝试添加自定义对象时发生错误

发布于 2024-12-15 01:52:18 字数 1635 浏览 4 评论 0原文

所以我有一个名为“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 技术交流群。

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

发布评论

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

评论(2

浮世清欢 2024-12-22 01:52:18

该异常告诉您它正在尝试将 -encodeWithCoder: 发送到您的 Box 实例。如果您查找 文档,可以看到这个属于NSCoding 协议。 NSCoding 由 NSKeyedArchiver 用于对对象进行编码。 NSDictionaryNSCoding 实现要求其内部的所有键和对象也符合 NSCoding。在您的情况下,存储在字典中的所有先前值都符合 NSCoding,但您的 Box 实例不符合。

您需要在您的 Box 类上实现 -initWithCoder:-encodeWithCoder: ,然后声明您的类符合 NSCoding 通过将声明修改为如下所示

@interface Box : Superclass <NSCoding>

The exception tells you that it's trying to send -encodeWithCoder: to your Box instance. If you look up the documentation, you can see that this belongs to the NSCoding protocol. NSCoding is used by NSKeyedArchiver to encode objects. NSDictionary's implementation of NSCoding requires that all keys and objects inside it also conform to NSCoding. In your case, all the previous values stored in your dictionary conform to NSCoding, but your Box instance doesn't.

You need to implement -initWithCoder: and -encodeWithCoder: on your Box class and then declare that your class conforms to NSCoding by modifying the declaration to look like

@interface Box : Superclass <NSCoding>
如果没有 2024-12-22 01:52:18

您的自定义 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.

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