**
我正在将表单中的数据导入数组,并尝试使用NSKeyedArchiver.
**
@implementation Player
- (id)init
{
self = [super init];
if (self) {
// Initialization code here.
}
return self;
}
- (void)dealloc
{
[super dealloc];
}
- (IBAction)savePlayer:(id)sender {
NSString *path = @"/Users/username/fm.plist";
NSString *pl= [teamPlayer stringValue];
NSString *name = [namePlayer stringValue];
NSString *age = [agePlayer stringValue];
NSString *position= [positionPlayer stringValue];
Player *player = [[Player alloc] init];
array = [[NSMutableArray alloc] initWithObjects:pl,
name, age, position, nil];
[NSKeyedArchiver archiveRootObject:array toFile:path];
NSString *ns = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
NSLog(@"test: %@" , ns);
[array release];
}-
- (void) encodeWithCoder: (NSCoder *) coder{
[coder encodeObject:array forKey:@"someArray"];
}
- (void) decodeWithCoder: (NSCoder *) coder{
[coder decodeObjectForKey:@"someArray"];
return self;
}发布于 2011-06-05 13:18:36
首先,后两个方法似乎不相关,因为您没有编码这些方法所属的任何对象。同样,NSCoding协议包括encodeWithCoder:和initWithCoder:方法。在decodeWithCoder:协议中没有NSCoding方法。
其次,您正在创建一个新的NSMutableArray对象,该对象初始化了几个元素,并将其存档到一个文件中,以便它在现有的文件上进行写入。您需要通过解压缩文件来获取现有的数组,创建一个可变的副本,然后追加这些值。所以代码就像这样,
NSArray *existingValues = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
NSArray *newValues = [existingValues copy];
[newValues addObjectsFromArray:[NSArray arrayWithObjects:pl, name, age, position, nil]];
[NSKeyedArchiver archiveRootObject:newValues toFile:path];
[newValues release];https://stackoverflow.com/questions/6242942
复制相似问题