使用 NSCoder 保存自己的类
我正在尝试将一些自定义类/数据存储到我的 iPhone/iPad 应用程序中的文件中。
我有一个 RSHighscoreList 类
@interface RSHighscoreList : NSObject {
NSMutableArray *list;
}
,其中包含列表中的 RSHighscore 对象
@interface RSHighscore : NSObject {
NSString *playerName;
NSInteger points;
}
当我尝试将所有内容存储到文件时,
- (void)writeDataStore {
RSDataStore *tmpStore = [[RSDataStore alloc] init];
_tmpStore.highscorelist = self.highscorelist.list;
NSMutableData *data = [[NSMutableData alloc] init];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[archiver encodeObject:tmpStore forKey:kDataKey];
[archiver finishEncoding];
[data writeToFile:[self dataFilePath] atomically:YES];
[archiver release];
[data release];
}
@interface RSDataStore : NSObject <NSCoding, NSCopying> {
NSMutableArray *highscorelist;
}
- (void)encodeWithCoder:(NSCoder *)encoder {
[encoder encodeObject:highscorelist forKey:@"Highscorelist"];
}
应用程序将崩溃并显示错误消息
-[RSHighscoreencodeWithCoder:]: 无法识别的选择器发送到实例 0x573cc20 *** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[RSHighscoreencodeWithCoder:]:无法识别的选择器发送到实例 0x573cc20”
我想知道为什么错误会告诉有关 RSHighscore 的信息,即使它是“包装的” 。有人有好主意吗?
I'm trying to store some custom class/data to a file in my iPhone/iPad app.
I have a Class RSHighscoreList
@interface RSHighscoreList : NSObject {
NSMutableArray *list;
}
which contains objects of RSHighscore in the list
@interface RSHighscore : NSObject {
NSString *playerName;
NSInteger points;
}
When I try to store all to file
- (void)writeDataStore {
RSDataStore *tmpStore = [[RSDataStore alloc] init];
_tmpStore.highscorelist = self.highscorelist.list;
NSMutableData *data = [[NSMutableData alloc] init];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[archiver encodeObject:tmpStore forKey:kDataKey];
[archiver finishEncoding];
[data writeToFile:[self dataFilePath] atomically:YES];
[archiver release];
[data release];
}
@interface RSDataStore : NSObject <NSCoding, NSCopying> {
NSMutableArray *highscorelist;
}
- (void)encodeWithCoder:(NSCoder *)encoder {
[encoder encodeObject:highscorelist forKey:@"Highscorelist"];
}
The app will crash with an error message
-[RSHighscore encodeWithCoder:]: unrecognized selector sent to instance 0x573cc20 *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[RSHighscore encodeWithCoder:]: unrecognized selector sent to instance 0x573cc20'
I wonder why the error tells about RSHighscore, even if that is 'wrapped'. Does anyone have a good idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
RSDataStore
有一个-encodeWithCoder:
方法,但(根据错误消息)RSHighscore
没有。您需要实现 NSCoding 您要序列化的每个类的协议。如果
RSHighscore
的基类更改为NSObject
以外的其他内容,则可能需要更改-initWithCoder:
方法以调用>[super initWithCoder:decoder]
而不是[super init]
。或者,将
添加到 NSObject 并立即更改RSHighscore
的-initWithCoder:
。RSDataStore
has an-encodeWithCoder:
method, but (according to the error message)RSHighscore
doesn't. You need to implement the NSCoding protocol for every class you're serializing.If the base class of
RSHighscore
is ever changed to something other thanNSObject
, the-initWithCoder:
method might need to be changed to call[super initWithCoder:decoder]
rather than[super init]
. Alternatively, add<NSCoding>
to NSObject and changeRSHighscore
's-initWithCoder:
now.您要编码或 initWithCoder 的类应符合
协议因此,您应该将其添加到界面中,否则运行时实际上将无法识别选择器,因为它是
协议的一部分The class you're going to encode or initWithCoder should conform to
<NSCoding>
protocolSo you just should add this in your interface, otherwise indeed the runtime will not recognize the selector as it's the part of
<NSCoding>
protocol