-
//读取plist
-
-
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"plistdemo" ofType:@"plist"];
-
NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
-
NSLog(@"%@", data);
-
-
//添加一项内容
-
[data setObject:@"add some content" forKey:@"c_key"];
-
-
//获取应用程序沙盒的Documents目录
-
NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
-
NSString *plistPath1 = [paths objectAtIndex:0];
-
-
//得到完整的文件名
-
NSString *filename=[plistPath1 stringByAppendingPathComponent:@"test.plist"];
-
//输入写入
-
[data writeToFile:filename atomically:YES];
-
-
//那怎么证明我的数据写入了呢?读出来看看
-
NSMutableDictionary *data1 = [[NSMutableDictionary alloc] initWithContentsOfFile:filename];
-
NSLog(@"%@", data1);
使用Xcode自带的创建pilst文件功能,创建ErrorCode.plist
如图:
2.在项目中使用plist文件
NSString *strErrorCodePath = [[NSBundle mainBundle]pathForResource:@"ErrorCode" ofType:@"plist"];
NSDictionary *dictErrorCode = [NSDictionary dictionaryWithContentsOfURL:[NSURLfileURLWithPath:strErrorCodePath]];
NSDictionary *dictErrorPromp = [dictErrorCode objectForKey:@"ErrorPromp"];
if (![self.msgCode isEqualToString:@""] && self.msgCode != nil) {
NSString *strPromp = [dictErrorPromp objectForKey:self.msgCode];
if (![strPromp isEqualToString:@""] && strPromp != nil) {
[LoadingView showLoadingView:strPromp];
}
}
创建 PS_CONFIG.plist 文件,截图如下:
将PS_CONFIG.plist的使用方法创建一个单独的类处理
PSConfig.h文件
#import <Foundation/Foundation.h>
@interface PSConfig : NSObject {
}
+ (NSString*)getConfigFilePath;
+ (NSString*)getDocumentConfigFilePath;
+ (void)setDeviceTokenValue:(NSString *)value;
+ (NSString *)getDeviceTokenValue;
+ (void)setIsFirstInstallValue:(BOOL)value;
+ (BOOL)getIsFirstInstallValue;
+ (void)setIsLookedCheckOrderGuideValue:(BOOL)value;
+ (BOOL)getIsLookedCheckOrderGuideValue;
+ (void)setIsLookedCheckOrderGuide2Value:(BOOL)value;
+ (BOOL)getIsLookedCheckOrderGuide2Value;
+ (void)setIsLookedUserAddressGuideValue:(BOOL)value;
+ (BOOL)getIsLookedUserAddressGuideValue;
+ (void)setLastUpdateTimeValue:(NSString *)value;
+ (NSString *)getLastUpdateTimeValue;
@end
PSConfig.m文件
#import "PSConfig.h"
#define kConfigFileName @"PS_CONFIG"
#define kConfigFileType @"plist"
@implementation PSConfig
// 原始文件目录
+ (NSString*)getConfigFilePath{
NSString *configFilePath = [[NSBundlemainBundle]pathForResource:kConfigFileNameofType:kConfigFileType];
return configFilePath;
}
// 用来作更改的目录
+ (NSString*)getDocumentConfigFilePath{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectorystringByAppendingPathComponent:@"PS_CONFIG.plist"];
NSFileManager *fileManager = [NSFileManagerdefaultManager];
BOOL isExist = [fileManager fileExistsAtPath:path];
if (!isExist) {
NSString *configFilePath = [self getConfigFilePath];
NSDictionary *configDic = [NSDictionarydictionaryWithContentsOfFile:configFilePath];
[configDic writeToFile:path atomically:YES];
}
return path;
}
+ (void)setDeviceTokenValue:(NSString *)value{
NSString *confi