我有一个有这两个ivars的课程:
@interface UserData : NSObject <NSCoding> {
NSString *name;
NSNetService *service;
}
我想最终将其编码为NSData对象,因此我实现了NSCoding:
- (id)initWithCoder:(NSCoder *)aDecoder {
if ((self = [super init])) {
name = [[aDecoder decodeObjectForKey:@"name"] copy];
service = [[aDecoder decodeObjectForKey:@"service"] copy];
}
return self;
}
- (void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:name forKey:@"name"];
[aCoder encodeObject:service forKey:@"service"];
}
但是,当我尝试使用archivedDataWithRootObject
时,会出现错误:
- [NSNetService encodeWithCoder:]:发送到实例的无法识别的选择器 0x100170c40
- 处理键输入时检测到异常。 - [NSNetService encodeWithCoder:]:发送到实例的无法识别的选择器 0x100170c40
- * - [NSKeyedArchiver dealloc]:警告:NSKeyedArchiver已取消分配 没有-finishEncoding 呼吁它。
如何让NSNetService
使用NSCoding?