이전 스타일의 encodeObject :/decodeObject NSCoder 직렬화를 사용하는 프로젝트가 있습니다. serialization 프로세스에 새 객체를 추가해야하지만 decodeObject가 아직없는 객체를 디코딩하려고하면 이전에 저장 한 문서가 충돌합니다.NSCoder를 사용하여 추가 객체를 추가해야합니다. encodeObject :/decodeObject
-(void) encodeWithCoder:(NSCoder *)aCoder {
[super encodeWithCoder:aCoder];
[aCoder encodeObject:existingArray];
[aCoder encodeObject:anotherExistingArray];
[aCoder encodeObject:myNewArray];
}
-(id) initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if(self != nil) {
NSArray *_existingArray = [aDecoder decodeObject];
NSArray *_anotherExistingArray = [aDecoder decodeObject];
// We crash here, this object doesn't exist in documents saved with only the first two arrays
NSArray *_myNewArray = [aDecoder decodeObject];
}
return(self);
}
나는 키있는 아카이브에이 변환하고 (요구 사항입니다) 이전에 저장된 아카이브와의 호환성을 유지할 수 있다고 생각하지 않습니다. 던져진 예외를 잡으려고하는 것은 도움이되지 않습니다.
아이디어가 있으십니까?
업데이트 : : Bah, 질문을하면 즉시 답변을 찾을 수 있습니다.
이 클래스의 버전이 올바르게 지정되었습니다. 이전 저자는 버전 확인에 버그가있었습니다. 따라서 완전성을 위해 :
+ (void) initialize {
if(self == [MyClass class]) {
[self setVersion:2]; // was 1
}
}
-(void) encodeWithCoder:(NSCoder *)aCoder {
[super encodeWithCoder:aCoder];
[aCoder encodeObject:existingArray];
[aCoder encodeObject:anotherExistingArray];
[aCoder encodeObject:myNewArray];
}
-(id) initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
int version = [aDecoder versionForClassName:@"MyClass"];
if(self != nil) {
NSArray *_existingArray = [aDecoder decodeObject];
NSArray *_anotherExistingArray = [aDecoder decodeObject];
if(version > 1) {
NSArray *_myNewArray = [aDecoder decodeObject];
}
}
return(self);
}
개체가 올바르게 버전되지 않는 경우가 합리적으로 좋은 생각으로 마크의 답변을 수락. – MyztikJenz