[英]Can't load array from plist,not getting objects
我试图将分数(NSNumber)保存到plist文件中,然后加载它。 这是我的保存方法:
- (void)saveScore:(int)iScore;
{
NSMutableData *data = [[NSMutableData alloc] init];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
NSNumber *score=[NSNumber numberWithInt:iScore];
[archiver encodeObject:[NSArray arrayWithObject:score]];
[archiver finishEncoding];
[data writeToFile:[self dataFilePath] atomically:YES];
}
我检查了它,并将数据保存到文件中。
这是我的加载方法:
-(void)LoadData{
NSString *filePath = [self dataFilePath];
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath])
{
NSMutableArray *array = [[NSMutableArray alloc] initWithContentsOfFile:filePath];
NSNumber * number= ((NSNumber *)[array objectAtIndex:0]);
int score=[number intValue];
}
}
我检查了,dataFilePath返回正确的路径。 但阵列没有得到任何物品。
如果使用NSKeyedArchiver
创建文件,则需要使用NSKeyedUnarchiver
来重新读取文件。
如果你想使用[[NSMutableArray alloc] initWithContentsOfFile:...
来读取文件,你应该使用[data writeToFile:path atomically:YES];
将数组保存到文件中[data writeToFile:path atomically:YES];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.