目标C:我试图保存包含原语和对象的NSDictionary,但是一旦我尝试添加自定义对象 [英] Objective C: I'm trying to save an NSDictionary that contains primitives and object, but error occurred once i tried adding the custom object

查看:110
本文介绍了目标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中。在盒子不在那里之前,一切工作都很好,但是当我添加自定义对象Box时,它不能再保存了。

   - (void)saveDataToDisk:(NSMutableDictionary *)dictionaryForDisk 
{
NSMutableData * data = [NSMutableData数据];
NSKeyedArchiver * archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
//编码
[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];
}

这是错误



发送到实例0x4e30d60
2011-11-11 11:56:43.430耐克[1186:207] ***终止应用程序由于未捕获的异常'NSInvalidArgumentException',原因:' - [Box encodeWithCoder:]:无法识别的选择器发送到实例0x4e30d60'
***首次调用堆栈
/ pre>

任何帮助将不胜感激!!!!

解决方案

p>异常告诉你,它试图将 -encodeWithCoder:发送到你的实例。如果您查找文档,您可以看到这属于 NSCoding 协议。 NSCoding NSKeyedArchiver 用于编码对象。 NSDictionary 执行 NSCoding 要求其内的所有键和对象也符合 NSCoding 。在您的情况下,存储在字典中的所有以前的值都符合 NSCoding ,但是您的 Box 实例不存在。



您需要实现 -initWithCoder: -encodeWithCoder:您的类,然后声明您的类符合 NSCoding 通过修改声明看起来像



  @interface框:超类< NSCoding> 


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!!!!

解决方案

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>

这篇关于目标C:我试图保存包含原语和对象的NSDictionary,但是一旦我尝试添加自定义对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆