从plist文件创建和写入/读取不起作用

时间:2014-01-13 01:52:43

标签: objective-c xcode cocoa-touch

我正在尝试保存多个游戏,每当我想保存一个时,第一个方法被调用。但是,无论我做什么,[NSFileManager defaultManager]都会一直告诉我[SaveGameManager filePath]处没有文件,而[SaveGameManager savedGames]一直给我零个对象。有什么建议/帮助吗?

@implementation SaveGameManager

+(void)saveGameWithDate:(NSDate*)date type:(NSInteger)type allMoves:(NSArray*)allMoves players:(NSArray*)players playerTimes:(NSArray*)playerTimes delayTime:(double)delayTime useTimer:(BOOL)useTimer delayOption:(NSInteger)delayOption drawOfferState:(NSInteger)drawOfferState resignationState:(NSInteger)resignationState current:(BOOL)current {
    NSLog(@"saving game");
    NSMutableArray* savedGames = [SaveGameManager savedGames];
    if (!savedGames) {
        savedGames = [NSMutableArray array];
    }

    NSMutableDictionary* dict = [@{@"date" : date, @"boardType" : @(type), @"allMoves" : allMoves, @"players" : players, @"delayTime" : @(delayTime), @"useTimer" : @(useTimer), @"delayOption" : @(delayOption), @"drawOfferState" : @(drawOfferState), @"resignationState" : @(resignationState), @"current" : @(current)} mutableCopy];

    if (playerTimes) {
        dict[@"playerTimes"] = playerTimes;
    }

    [savedGames addObject:dict];

    [savedGames writeToFile:[SaveGameManager filePath] atomically:YES];
}

+(NSString*)filePath {
    static NSString* path;

    if (!path) {
        path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:@"SavedGames.plist"];
    }

    return path;
}

+(NSMutableArray*)savedGames {
    return [NSMutableArray arrayWithContentsOfFile:[SaveGameManager filePath]];
}

@end

1 个答案:

答案 0 :(得分:2)

使用此代码在应用程序文档中以Plist格式保存NSDictionary。

+ (void)saveSessionToDisk:(NSDictionary *)session {
    NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] initWithDictionary:session];
    NSError *error;
    NSData *archiveData = [NSKeyedArchiver archivedDataWithRootObject:dictionary];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [paths objectAtIndex:0];
    NSString *fullPath = [documentsDir stringByAppendingPathComponent:@"SavedSession.plist"];
    [archiveData writeToFile:fullPath options:NSDataWritingAtomic error:&error];
}

使用此代码从您之前保存的Plist加载NSDictionary。

+ (NSDictionary *)loadSessionFromDisk
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [paths objectAtIndex:0];
    NSString *fullPath = [documentsDir stringByAppendingPathComponent:@"SavedSession.plist"];
    DLog(@"%@",fullPath);
    NSFileManager *fileManager =[NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:fullPath]) {
        NSMutableDictionary *dict = nil;
        @try {
            NSData *archiveData = [NSData dataWithContentsOfFile:fullPath];
            dict = (NSMutableDictionary*)[NSKeyedUnarchiver unarchiveObjectWithData:archiveData];
            if ([dict count] > 0) {
                return dict;
            } else {
                return nil;
            }
        } @catch (NSException *e) {
        }
    } else {
        return nil;
    }
}

在尝试加载NSDictionary之前,请始终使用此条件:

if ([[UtilityFunctions loadSessionFromDisk] isKindOfClass:[NSDictionary class]]) {
    NSDictionary *myDictionary = [UtilityFunctions loadSessionFromDisk];
}