如何编码 NSNetService?
我有一个具有这两个 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
时,它给出了错误:
-[NSNetServiceencodeWithCoder:]:无法识别的选择器发送到实例 0x100170c40
-处理按键输入时检测到异常。 -[NSNetServiceencodeWithCoder:]:无法识别的选择器发送到实例 0x100170c40
-* -[NSKeyedArchiver dealloc]:警告:NSKeyedArchiver 已释放 没有 -finishEncoding 呼吁它。
如何使 NSNetService
与 NSCoding 一起使用?
I have a class that has these two ivars:
@interface UserData : NSObject <NSCoding> {
NSString *name;
NSNetService *service;
}
I wanted to encode this into a NSData object eventually, so I implemented 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"];
}
However, when I try to use archivedDataWithRootObject
it gives the errors:
-[NSNetService encodeWithCoder:]: unrecognized selector sent to instance
0x100170c40-Exception detected while handling key input.
-[NSNetService encodeWithCoder:]: unrecognized selector sent to instance
0x100170c40-* -[NSKeyedArchiver dealloc]: warning: NSKeyedArchiver deallocated
without having had -finishEncoding
called on it.
How can I make NSNetService
work with NSCoding?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论