와 나는 NSCoding 프로토콜을 구현하는 내 사람 클래스가 : 인코딩 NSMutableArray를 부분적으로 NSKeyedArchiver
@interface Person : NSObject <NSCoding>
@property NSString *name;
@property float salary;
@end
@implementation Person
-(void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:_name forKey:@"name"];
[aCoder encodeFloat:_salary forKey:@"salary"];
}
-(id)initWithCoder:(NSCoder *)aDecoder{
if(self=[super init])
{
_name =[aDecoder decodeObjectForKey:@"name"];
_salary=[aDecoder decodeFloatForKey:@"salary"];
}
return self;
}
이 또한 내가 직렬화하고 싶은 사람, 배열을 가지고있다. 그러나 모든 엔티티가 아닙니다. 나는 부분적으로 그것을 저장하고 싶다.
@interface customNSMutableArray : NSMutableArray<NSCoding>
@end
@implementation customNSMutableArray
-(void)encodeWithCoder:(NSCoder *)aCoder
{
for(NSObject * obj in self)
{
//if(some_condition)
[aCoder encodeObject:obj];
}
}
-(id)initWithCoder:(NSCoder *)aDecoder
{
//@todo
return self;
}
@end
을 그리고 여기에 문제가 있습니다 : 나는 방법 initWithCoder NSCoding 프로토콜과 그것의 자신의 encodeWithCoder 및 을 구현하는 것이다 NSMutableArray를내 자신의 서브 클래스를 생성하기로 결정했다. 내 customNSMutableArray를 인코딩하는 시도 후 : 내 customNSMutableArray에
@property customNSMutableArray *companyStaff;
...
NSData * data=[NSKeyedArchiver archivedDataWithRootObject:_companyStaff];
encodeWithCoder 방법은 호출되지 않습니다. 직접 으로 전달됩니다. 내 인물 배열 내의 엔티티 인 encodeWithCoder입니다.
어떻게 부분적으로 NSMutableArray를 직렬화 할 수 있습니까?
NSMutableArray 하위 클래스를 만드는 것은 좋지 않습니다. 그것은 "클래스 클러스터"의 일부입니다. 그리고 이것을 위해 어쨌든 서브 클래스를 만들 이유가 없습니다. 그냥'NSMutableArray' 전체를 archive/unarchive하십시오. – rmaddy