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 ); }
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文件
创建过程:
完成创建。
手动加数据, 如图:
先随便加入一些数据如图:
读取:
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; }; }