使用Save- / Open-Panel保存NSNumber数组

时间:2015-11-06 11:36:44

标签: objective-c nsmutablearray nsnumber nskeyedarchiver nskeyedunarchiver

我正在尝试保存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

0 个答案:

没有答案