简体   繁体   English

无法从plist加载数组,也无法获取对象

[英]Can't load array from plist,not getting objects

Im trying to save a score (NSNumber) into a plist file and then load it. 我试图将分数(NSNumber)保存到plist文件中,然后加载它。 This is my save method: 这是我的保存方法:

- (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];

}

which i checked and it saves the data into the file. 我检查了它,并将数据保存到文件中。

this is my load method: 这是我的加载方法:

-(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];
    }
}

i checked and the dataFilePath returns the right path. 我检查了,dataFilePath返回正确的路径。 but the array don't get any items. 但阵列没有得到任何物品。

If you use an NSKeyedArchiver to create a file, you need to use an NSKeyedUnarchiver to read the file back in. 如果使用NSKeyedArchiver创建文件,则需要使用NSKeyedUnarchiver来重新读取文件。

If you want to use [[NSMutableArray alloc] initWithContentsOfFile:... to read the file in then you should save the array to file using [data writeToFile:path atomically:YES]; 如果你想使用[[NSMutableArray alloc] initWithContentsOfFile:...来读取文件,你应该使用[data writeToFile:path atomically:YES];将数组保存到文件中[data writeToFile:path atomically:YES];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM