</pre><pre name="code" class="objc"> NSString *plistPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
//得到完整的文件名
NSString *filename=[plistPath stringByAppendingPathComponent:@"searchHistory.plist"];
NSMutableArray *historyArr = [[[NSMutableArray alloc]initWithContentsOfFile:filename]mutableCopy];
//NSMutableDictionary *historyArr = [[[NSMutableDictionary alloc]initWithContentsOfFile:filename]mutableCopy];
// NSMutableArray *historyArr = [applist objectForKey:@"history"];
if (!historyArr)
{
historyArr = [NSMutableArray arrayWithCapacity:0];
}//如果数组里没有,就把搜索关键字存进去
if ([historyArr indexOfObject:searchBar.text] == NSNotFound)
{
[historyArr addObject:searchBar.text];
// NSMutableArray *data=[[NSMutableArray alloc]init];
//[data setObject:historyArr forKey:@"history"];
//[data writeToFile:filename atomically:YES];
[historyArr writeToFile:filename atomically:YES];
};
<pre name="code" class="objc">将Json存进本地文件夹
NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path=[paths objectAtIndex:0];
NSString *Json_path=[path stringByAppendingPathComponent:@"JsonFile.json"];
//==写入文件
NSLog(@"%@",[JsonData writeToFile:Json_path atomically:YES] ? @"Succeed":@"Failed");
-----------------------
读取Json
//==Json文件路径
NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path=[paths objectAtIndex:0];
NSString *Json_path=[path stringByAppendingPathComponent:@"JsonFile.json"];
//==Json数据
NSData *data=[NSData dataWithContentsOfFile:Json_path];
//==JsonObject
id JsonObject=[NSJSONSerialization JSONObjectWithData:data
options:NSJSONReadingAllowFragments
error:&error]
//nslog (@"%@",jsonobject);打印json字典
plist文件与json的存储与读取
最新推荐文章于 2023-04-17 17:21:50 发布