我正在尝试保存一个 NSNumber 数组,但我不知道该怎么做。
我的代码适用于单个 NSNumber:
- (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError {
return [NSKeyedArchiver archivedDataWithRootObject:Number];
}
- (BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName error:(NSError **)outError {
[self setNumber:[NSKeyedUnarchiver unarchiveObjectWithData:data]];
return YES;
}
/*
- (void)encodeWithCoder:(NSCoder *)coder {
[coder encodeObject:Number forKey:@"number"];
} */
/*
- (instancetype)initWithCoder:(NSCoder *)coder {
if (self) {
self.Number = [coder decodeObjectForKey:@"number"];
}
return self;
} */
由于它不适用于 NSNumber 数组,因此我尝试使用 NSMutableArray 代替,但它也不起作用:
- (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError {
return [NSKeyedArchiver archivedDataWithRootObject:Array];
}
- (BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName error:(NSError **)outError {
[self setArray:[NSKeyedUnarchiver unarchiveObjectWithData:data]];
return YES;
}
/*
- (void)encodeWithCoder:(NSCoder *)coder {
[coder encodeObject:Array[0] forKey:@"one"];
[coder encodeObject:Array[1] forKey:@"two"];
[coder encodeObject:Array[2] forKey:@"three"];
} */
/*
- (instancetype)initWithCoder:(NSCoder *)coder {
if (self) {
self.Array[0] = [coder decodeObjectForKey:@"one"];
self.Array[1] = [coder decodeObjectForKey:@"two"];
self.Array[2] = [coder decodeObjectForKey:@"three"];
}
return self;
} */
那么我做错了什么?
编辑:似乎不需要 encodeWithCoder 和 initWithCoder