ISO之plist读写操作

1、读取Info.plist(注意I是大写,并且这里大小写敏感,坑)

    NSString * infoplistPath = [[NSBundle mainBundle] pathForResource:@"Info" ofType:@"plist"];
    NSMutableDictionary * infoDict = [[NSMutableDictionary alloc] initWithContentsOfFile:infoplistPath];
    NSLog(@"%@", infoDict);

读取信息:

2015-11-11 15:01:05.918 01-plist[2745:140559] {
    BuildMachineOSBuild = 14F1021;
    CFBundleDevelopmentRegion = en;
    CFBundleExecutable = "01-plist";
    CFBundleIdentifier = "dzl.-1-plist";
    CFBundleInfoDictionaryVersion = "6.0";
    CFBundleName = "01-plist";
    CFBundlePackageType = APPL;
    CFBundleShortVersionString = "1.0";
    CFBundleSignature = "????";
    CFBundleSupportedPlatforms =     (
        iPhoneSimulator
    );
    CFBundleVersion = 1;
    DTCompiler = "com.apple.compilers.llvm.clang.1_0";
    DTPlatformBuild = "";
    DTPlatformName = iphonesimulator;
    DTPlatformVersion = "9.1";
    DTSDKBuild = 13B5110e;
    DTSDKName = "iphonesimulator9.1";
    DTXcode = 0710;
    DTXcodeBuild = 7B60;
    LSRequiresIPhoneOS = 1;
    MinimumOSVersion = "9.1";
    UIDeviceFamily =     (
        1,
        2
    );
    UILaunchStoryboardName = LaunchScreen;
    UIMainStoryboardFile = Main;
    UIRequiredDeviceCapabilities =     (
        armv7
    );
    UISupportedInterfaceOrientations =     (
        UIInterfaceOrientationPortrait,
        UIInterfaceOrientationLandscapeLeft,
        UIInterfaceOrientationLandscapeRight
    );
    "UISupportedInterfaceOrientations~ipad" =     (
        UIInterfaceOrientationPortrait,
        UIInterfaceOrientationPortraitUpsideDown,
        UIInterfaceOrientationLandscapeLeft,
        UIInterfaceOrientationLandscapeRight
    );
}

2、向Info.plist里面写数据

    NSString * infoplistPath = [[NSBundle mainBundle] pathForResource:@"Info" ofType:@"plist"];
    NSMutableDictionary * infoDict = [[NSMutableDictionary alloc] initWithContentsOfFile:infoplistPath];
    NSLog(@"%@", infoDict);
    
    
    [infoDict setObject:@"test add content" forKey:@"testAddContent"];
    //atomically yes保证写入文件的原子性,就是先创建一个临时文件,直到文件内容写入成功再导入到目标文件中
    //           no 直接写入到目标文件
    [infoDict writeToFile:infoplistPath atomically:YES];
    
    
    NSMutableDictionary * subInfoDict = [[NSMutableDictionary alloc] initWithContentsOfFile:infoplistPath];
    NSLog(@"%@", subInfoDict);

为了证明写入了数据,再读一遍输出:

2015-11-11 15:22:41.075 01-plist[2880:148023] {
    BuildMachineOSBuild = 14F1021;
    CFBundleDevelopmentRegion = en;
    CFBundleExecutable = "01-plist";
    CFBundleIdentifier = "dzl.-1-plist";
    CFBundleInfoDictionaryVersion = "6.0";
    CFBundleName = "01-plist";
    CFBundlePackageType = APPL;
    CFBundleShortVersionString = "1.0";
    CFBundleSignature = "????";
    CFBundleSupportedPlatforms =     (
        iPhoneSimulator
    );
    CFBundleVersion = 1;
    DTCompiler = "com.apple.compilers.llvm.clang.1_0";
    DTPlatformBuild = "";
    DTPlatformName = iphonesimulator;
    DTPlatformVersion = "9.1";
    DTSDKBuild = 13B5110e;
    DTSDKName = "iphonesimulator9.1";
    DTXcode = 0710;
    DTXcodeBuild = 7B60;
    LSRequiresIPhoneOS = 1;
    MinimumOSVersion = "9.1";
    UIDeviceFamily =     (
        1,
        2
    );
    UILaunchStoryboardName = LaunchScreen;
    UIMainStoryboardFile = Main;
    UIRequiredDeviceCapabilities =     (
        armv7
    );
    UISupportedInterfaceOrientations =     (
        UIInterfaceOrientationPortrait,
        UIInterfaceOrientationLandscapeLeft,
        UIInterfaceOrientationLandscapeRight
    );
    "UISupportedInterfaceOrientations~ipad" =     (
        UIInterfaceOrientationPortrait,
        UIInterfaceOrientationPortraitUpsideDown,
        UIInterfaceOrientationLandscapeLeft,
        UIInterfaceOrientationLandscapeRight
    );
}
2015-11-11 15:22:41.078 01-plist[2880:148023] {
    BuildMachineOSBuild = 14F1021;
    CFBundleDevelopmentRegion = en;
    CFBundleExecutable = "01-plist";
    CFBundleIdentifier = "dzl.-1-plist";
    CFBundleInfoDictionaryVersion = "6.0";
    CFBundleName = "01-plist";
    CFBundlePackageType = APPL;
    CFBundleShortVersionString = "1.0";
    CFBundleSignature = "????";
    CFBundleSupportedPlatforms =     (
        iPhoneSimulator
    );
    CFBundleVersion = 1;
    DTCompiler = "com.apple.compilers.llvm.clang.1_0";
    DTPlatformBuild = "";
    DTPlatformName = iphonesimulator;
    DTPlatformVersion = "9.1";
    DTSDKBuild = 13B5110e;
    DTSDKName = "iphonesimulator9.1";
    DTXcode = 0710;
    DTXcodeBuild = 7B60;
    LSRequiresIPhoneOS = 1;
    MinimumOSVersion = "9.1";
    UIDeviceFamily =     (
        1,
        2
    );
    UILaunchStoryboardName = LaunchScreen;
    UIMainStoryboardFile = Main;
    UIRequiredDeviceCapabilities =     (
        armv7
    );
    UISupportedInterfaceOrientations =     (
        UIInterfaceOrientationPortrait,
        UIInterfaceOrientationLandscapeLeft,
        UIInterfaceOrientationLandscapeRight
    );
    "UISupportedInterfaceOrientations~ipad" =     (
        UIInterfaceOrientationPortrait,
        UIInterfaceOrientationPortraitUpsideDown,
        UIInterfaceOrientationLandscapeLeft,
        UIInterfaceOrientationLandscapeRight
    );
    testAddContent = "test add content";
}

有输出可见信息已经写入


3、自己创建一个plist文件

创建过程:

ISO之plist读写操作_第1张图片


ISO之plist读写操作_第2张图片


ISO之plist读写操作_第3张图片

完成创建。

手动加数据, 如图:

ISO之plist读写操作_第4张图片

先随便加入一些数据如图:

ISO之plist读写操作_第5张图片


读取:

    NSString * myPlistPath = [[NSBundle mainBundle] pathForResource:@"myPlist" ofType:@"plist"];
    NSMutableDictionary * dict = [[NSMutableDictionary alloc] initWithContentsOfFile:myPlistPath];
    NSLog(@"%@", dict);

输出:

2015-11-11 15:39:12.468 01-plist[2989:154789] {
    myData1 = "this is my data1";
    myData2 = "this is my data2";
    myData3 =     {
        dict1 = value1;
        dict2 = value2;
    };
}

这里就不在对这个文件追加了,跟在默认Info.plist里面追加是一样的。



你可能感兴趣的:(plist)