代码创建plist文件

用代码创建.plist文件

1.创建plist文件

NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

        NSString*documentsPath = [pathobjectAtIndex:0];

        NSString*plistPath = [documentsPathstringByAppendingPathComponent:@"mapParameter.plist"];



        // 判断plist是否存在

        NSFileManager*fileManager = [NSFileManagerdefaultManager];

        if([fileManagerfileExistsAtPath:plistPath] ==NO) {

            NSLog(@"================不存在");

            NSMutableDictionary *usersDic = [[NSMutableDictionary alloc ] init];

            //设置属性值

            [usersDicsetObject:@"1"forKey:@"condition"];

            [usersDicsetObject:@"1"forKey:@"point"];

            [usersDicsetObject:@"1"forKey:@"function"];

            //写入文件

            [usersDic writeToFile:plistPathatomically:YES]

        }else{

            NSLog(@"================存在");

            //读取plist文件

            NSMutableDictionary *dicM= [NSMutableDictionary dictionaryWithContentsOfFile:plistPath];

        }

&:写入的字典必须是可变字典,否则属性值会写不进去

2.修改plist

NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString*documentsPath = [pathobjectAtIndex:0];

    NSString*plistPath = [documentsPathstringByAppendingPathComponent:@"mapParameter.plist"];

    //写入文件

    [newDicM writeToFile:plistPath atomically:YES];

你可能感兴趣的:(代码创建plist文件)