Tôi biết rằng khi bạn viết phương thức initWithCoder của một lớp con của đối tượng thực hiện NSCoding bạn phải gọi siêu initWithCoder
(thay vì super init
), nhưng tôi có phải gọi super encodeWithCoder
trong việc triển khai encodeWithCoder không?Có cần gọi [super encodeWithCoder] khi subclassing một đối tượng thực hiện NSCoding không?
15
A
Trả lời
24
Nếu bạn kế thừa từ một lớp học có hỗ trợ mã hóa, nó là thường adviseable để sử dụng trong phương pháp [super encodeWithCoder:]
encodeWithCoder:
của bạn, càng nhiều càng tốt [super initWithCoder:]
được sử dụng trong phương pháp initWithCoder:
.
Tài liệu:NSCoding Protocol Reference
tham khảo:http://www.cocoadev.com/index.pl?NSCoder
nếu lớp kế thừa từ một lớp mà phù hợp với (NSObject không phù hợp) thì bạn nên bao gồm các [encodeWithCoder:] phương pháp.
// <NSCoding> protocol methods
-(void)encodeWithCoder:(NSCoder*)coder
{
[super encodeWithCoder:coder];
/*
[coder encodeObject: theNSStringInstanceVariable];
[coder encodeObject: theNSDictionaryInstanceVariable];
[coder encodeValueOfObjCType:@encode(BOOL) at:&theBooleanInstanceVariable];
[coder encodeValueOfObjCType:@encode(float) at:&theFloatInstanceVariable];
*/
}
Cảm ơn bạn rất nhiều :) – XaitormanX
@XaitormanX Xin chào, bạn của tôi! :-) –