ios 数据持久化之沙盒
2014-11-29 17:46
232 查看
1,访问沙盒路径
2,简单文件写入
//1,home主目录里面有,Documents,Library,tmp和一个应用程序 NSLog(@"Home:%@",NSHomeDirectory()); //2,Documents NSString *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0]; NSLog(@"%@",documentsPath); //3,Library NSString *libraryPath = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES)[0]; NSLog(@"%@",libraryPath); //4,tmp NSLog(@"tmp:%@",NSTemporaryDirectory()); //5,caches NSString *cachesPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0]; NSLog(@"%@",cachesPath); //6,user NSString *user = NSUserName(); NSLog(@"user:%@",user); //7,NSBundle:图片路径 NSString *bundle = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"png"]; NSLog(@"bundle:%@",bundle);
2,简单文件写入
//1,获取文件路径 NSString *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0]; NSLog(@"%@",documentsPath); //2,拼接文件路径 NSString *file = [documentsPath stringByAppendingString:@"/myText.txt"]; //3,准备写入的内容 NSString *content = @"Hello World!"; //4,写入(下面yes是原子性,整个写入,no有多少写多少) [content writeToFile:file atomically:YES encoding:NSUTF8StringEncoding error:nil]; //读取 NSString *readString = [NSString stringWithContentsOfFile:file encoding:NSUTF8StringEncoding error:nil]; NSLog(@"%@",readString);3,
//NSArray //1,获取document路径 //2,拼接文件路径 NSString *arrayPath = [documentsPath stringByAppendingString:@"/array.plist"]; //3,准备内容 NSArray *array = @[@"123",@"456",@"789"]; //4,写入 [array writeToFile:arrayPath atomically:YES]; //5,读取 NSArray *readArray = [NSArray arrayWithContentsOfFile:arrayPath]; NSLog(@"%@",readArray); //NSDictionary //1,获取文件路径 //2,拼接文件路径 NSString *dictPath = [documentsPath stringByAppendingString:@"/dict.plist"]; //3,准备写入的内容 NSDictionary *dict = @{@"name": @"张强",@"age":@"21"}; //4,写入内容 [dict writeToFile:dictPath atomically:YES]; //5,读取 NSDictionary *readDict = [NSDictionary dictionaryWithContentsOfFile:dictPath]; NSLog(@"%@",readDict);
相关文章推荐
- iOS数据持久化之写入沙盒
- iOS,沙盒,做数据持久化的时候,有时候需要查看沙盒路径
- iOS初级数据持久化 沙盒机制 归档与反归档
- iOS数据的持久化(沙盒)
- IOS中的数据持久化之沙盒篇
- [iOS]数据持久化-沙盒存储
- iOS中的UI数据持久化,沙盒
- iOS开发-数据持久化 :沙盒,归档反归档,NSFileManager文件管理器(单例)
- iOS初级数据持久化 沙盒机制 归档与反归档
- IOS 初级数据持久化-沙盒机制
- ios学习记录 day36 UI12初级数据持久化(沙盒)
- iOS中常用的四种数据持久化方法简介
- iOS中常用的四种数据持久化方法简介
- IOS持久化数据----(保存数据的一系列方法)
- iOS中常用的四种数据持久化方法简介
- iOS中常用的四种数据持久化方法简介
- iOS数据持久化-SQLite数据库使用详解
- iOS中常用的四种数据持久化方法
- iOS中常用的四种数据持久化方法简介
- iOS中常用的四种数据持久化方法