plist文件和字典互转

/**
写入字典到plist文件

@param dict 要写入的字典
@param path 要写入的路径
@return 是否写入成功
*/

+ (BOOL)saveDictionary:(NSDictionary *)dict isPlistFileOfPath:(NSString *)path {
    //判断dict是否是有效字典
    if (dict && [dict isKindOfClass:[NSDictionary class]]) {
        //dict有效
        return [dict writeToFile:path atomically:YES];
    } else {
        return NO;
    }
}

/**
从plist文件中读取字典

@param path 文件的路径
@return 读取到的字典
*/

+ (NSDictionary *)dictionaryInPlistFileOfPath:(NSString *)path {
    //判断文件是否存在
    if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
        //文件存在
        NSDictionary *dict = [[NSDictionary alloc]initWithContentsOfFile:path];
        if (dict && [dict isKindOfClass:[NSDictionary class]]) {
            //dict有效
            return dict;
        }else {
            return nil;
        }
    } else {
        return nil;
    }
}

你可能感兴趣的:(plist文件和字典互转)