2

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?

4

0 回答 0