1.写入数据
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *path = [paths objectAtIndex:0];
NSString *filename = [path stringByAppendingPathComponent:@"appLockRecord.plist"]; //获取路径
NSMutableDictionary* dataDic = [NSMutableDictionary dictionaryWithContentsOfFile:filename]; //读取数据
NSLog(@"dic2 is:%@",dataDic);
if (!dataDic) {
dataDic = [NSMutableDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:_dynamicId];
}else{
[dataDic setValue:[NSNumber numberWithBool:YES] forKey:_dynamicId];
}
[dataDic writeToFile:filename atomically:YES];
NSString *path = [paths objectAtIndex:0];
NSString *filename = [path stringByAppendingPathComponent:@"appLockRecord.plist"]; //获取路径
NSMutableDictionary* dataDic = [NSMutableDictionary dictionaryWithContentsOfFile:filename]; //读取数据
NSLog(@"dic2 is:%@",dataDic);
if (!dataDic) {
dataDic = [NSMutableDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:_dynamicId];
}else{
[dataDic setValue:[NSNumber numberWithBool:YES] forKey:_dynamicId];
}
[dataDic writeToFile:filename atomically:YES];
NSLog(@"dic2 is:%@",dataDic);
2.读取文件
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *path =[paths objectAtIndex:0];
NSLog(@"path = %@",path);
NSString *filename=[path stringByAppendingPathComponent:@"appLockRecord.plist"];
//读文件
NSDictionary* dic = [NSDictionary dictionaryWithContentsOfFile:filename];
if(dic == nil)
{
//1. 创建一个plist文件
NSFileManager* fm = [NSFileManager defaultManager];
[fm createFileAtPath:filename contents:nil attributes:nil];
}
NSLog(@"dic:%@",dic);
if ([[dic objectForKey:dynamicId]boolValue]) {
return YES;
}
NSString *path =[paths objectAtIndex:0];
NSLog(@"path = %@",path);
NSString *filename=[path stringByAppendingPathComponent:@"appLockRecord.plist"];
//读文件
NSDictionary* dic = [NSDictionary dictionaryWithContentsOfFile:filename];
if(dic == nil)
{
//1. 创建一个plist文件
NSFileManager* fm = [NSFileManager defaultManager];
[fm createFileAtPath:filename contents:nil attributes:nil];
}
NSLog(@"dic:%@",dic);
if ([[dic objectForKey:dynamicId]boolValue]) {
return YES;
}
return NO;
注意:记得把新增的数据 放到读取出来的字典里面,不然会被覆盖的