plist文件一般保存在沙盒目录下,且存储类型比较单一(具体包含哪些可谷歌、度娘),这里已plist读写字符串为例(真机、模拟器皆可用)
#pragma mark - plist 读取数据
-(NSArray *)historicalRecordReadTheData{
NSString * filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:@"historicalRecord.plist"];
NSMutableArray * historicalRecordArr = [[NSMutableArray alloc] initWithContentsOfFile:filePath];
return historicalRecordArr;
}
#pragma mark - plist 添加数据
-(void)historicalRecordAddData:(NSString *)string{
NSString * filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:@"historicalRecord.plist"];
NSMutableArray * historicalRecordArr = [[NSMutableArray alloc] initWithContentsOfFile:filePath];
if (!historicalRecordArr) {
historicalRecordArr = [[NSMutableArray alloc]init];
}
if(![historicalRecordArr containsObject:string]){
[historicalRecordArr addObject:string];
}
BOOL success = [historicalRecordArr writeToFile:filePath atomically:YES];
if (success) {
NSLog(@"写入成功");
}else{
NSLog(@"写入失败");
}
}
#pragma mark - plist 删除所有数据
-(void)historicalRecordstring{
NSString * filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:@"historicalRecord.plist"];
NSMutableArray * historicalRecordArr = [[NSMutableArray alloc] initWithContentsOfFile:filePath];
[historicalRecordArr removeAllObjects];
[historicalRecordArr writeToFile:filePath atomically:YES];
}
网友评论