UI中初级数据存储
2015-12-08 14:16
363 查看
NSString *homePath=NSHomeDirectory();
NSLog(@"homePath=%@",homePath);
#pragma marl ~~~~~~~~~~获取沙盒中子文件夹路径的方式一---拼接路径~~~~~
//获取Documents文件夹路径
NSString *document = [homePath
stringByAppendingPathComponent:@"/Documents"];
NSLog(@"document==%@",document);
NSString *library=[homePath
stringByAppendingPathComponent:@"/Library"];
NSLog(@"library=%@",library);
#pragma mark ~~~~~~~获取沙盒中子文件夹路径的方式二---通过函数获取
//获取Documents文件夹路径
NSString *doucumentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES)
lastObject];
NSLog(@"Documents==%@",doucumentPath);
//获取library路径
NSString *libraryPath=[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,
NSUserDomainMask, YES)lastObject];
NSLog(@"libraryPath==%@",libraryPath);
//获取Cache路径
NSString *Cache=[NSSearchPathForDirectoriesInDomains(NSCachesDirectory,
NSUserDomainMask, YES)lastObject];
// NSLog(@"Cache==%@",Cache);
//获取temp路径
NSString *temp=NSTemporaryDirectory();
NSLog(@"temp==%@",temp);
//应用程序路径
NSString *appPath=[[NSBundle
mainBundle] resourcePath];
NSLog(@"appPath======%@",appPath);
#pragma mark~~~~~~~~简单文件的写入/读取
/*
简单对象:NSString NSArray NSDictionary NSData
数据持久化:把简单数据写入到文件中,存储到沙河文件夹里
NSData:用于存储二进制数据
*/
#pragma mark~~~~~~~~~字符串对象的
写入
和 读取
//第一步:创建字符串对象
NSString *shi =
@" 静夜思\n
床前明月光,\n
疑是地上霜,\n 举头望明月,\n
低头思故乡。";
//第二步:构造字符串存储路径
NSString *strPath = [document
stringByAppendingPathComponent:@"/text.txt"];
//第三部:将文件写入指定路径
[shi writeToFile:strPath
atomically:YES
encoding:NSUTF8StringEncoding
error:nil];
NSLog(@"strPath==%@",strPath);
//读取txt
NSString *readText=[NSString
stringWithContentsOfFile:strPath encoding:NSUTF8StringEncoding
error:nil];
NSLog(@"readText=%@",readText);
#pragma mark~~~~~~~~~~数组对象的
写入和
读取
//构造一个数组对象
NSMutableArray *starArray=[NSMutableArray
arrayWithObjects:@"Kitty",@"Tom"@"Eid",@"Jack",@"Ilice",
nil];
//第二步:构建数组存储路径,创建.plist文件
NSString *arrayPath=[document
stringByAppendingPathComponent:@"/array.plist"];
//将数组写入plist
[starArray writeToFile:arrayPath
atomically:YES];
NSLog(@"arrayPath=%@",arrayPath);
//数组中再添加新元素
[starArray addObject:@"baby"];
[starArray writeToFile:arrayPath
atomically:YES];
NSLog(@"arrayPath==%@",arrayPath);
//读取
NSArray *readArray=[NSArray
arrayWithContentsOfFile:arrayPath];
NSLog(@"%@",readArray);
#pragma mark ~~~~~~~~~~~~字典对象的写入
//构造一个字典
语法堂
前面是key 后面是value
NSDictionary *dic=@{@"name":@"Kitty",@"age":@"18",@"sex":@"girl",@"hobby":@[@"sleep",@"eat"]};
//第二步:构造存储字典的plist文件的存储路径
NSString *dicPath = [document
stringByAppendingPathComponent:@"dic.plist"];
//第三部:写入
[dic writeToFile:dicPath
atomically:YES];
NSLog(@"dicPath=%@",dicPath);
//第四步:读取
NSDictionary *readDic=[NSDictionary
dictionaryWithContentsOfFile:dicPath];
NSLog(@"readDic=%@",readDic);
#pragma mark~~~~~~~~NSData对象的写入和读取
//第一步:创建图片
UIImage *image = [UIImage
imageNamed:@"head_19.jpg"];
//第二步:将图片转化为二进制
NSData *imageData=UIImageJPEGRepresentation(image,
0.5);
//构造NSData文件.da的存储路径
NSString *imagePath=[document
stringByAppendingPathComponent:@"/data.da"];
//第四步:写入
[imageData writeToFile:imagePath
atomically:YES];
NSLog(@"imagePath=%@",imagePath);
//读取
UIImage *readImage=[UIImage
imageWithContentsOfFile:imagePath];
NSLog(@"%@",readImage);
NSLog(@"homePath=%@",homePath);
#pragma marl ~~~~~~~~~~获取沙盒中子文件夹路径的方式一---拼接路径~~~~~
//获取Documents文件夹路径
NSString *document = [homePath
stringByAppendingPathComponent:@"/Documents"];
NSLog(@"document==%@",document);
NSString *library=[homePath
stringByAppendingPathComponent:@"/Library"];
NSLog(@"library=%@",library);
#pragma mark ~~~~~~~获取沙盒中子文件夹路径的方式二---通过函数获取
//获取Documents文件夹路径
NSString *doucumentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES)
lastObject];
NSLog(@"Documents==%@",doucumentPath);
//获取library路径
NSString *libraryPath=[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,
NSUserDomainMask, YES)lastObject];
NSLog(@"libraryPath==%@",libraryPath);
//获取Cache路径
NSString *Cache=[NSSearchPathForDirectoriesInDomains(NSCachesDirectory,
NSUserDomainMask, YES)lastObject];
// NSLog(@"Cache==%@",Cache);
//获取temp路径
NSString *temp=NSTemporaryDirectory();
NSLog(@"temp==%@",temp);
//应用程序路径
NSString *appPath=[[NSBundle
mainBundle] resourcePath];
NSLog(@"appPath======%@",appPath);
#pragma mark~~~~~~~~简单文件的写入/读取
/*
简单对象:NSString NSArray NSDictionary NSData
数据持久化:把简单数据写入到文件中,存储到沙河文件夹里
NSData:用于存储二进制数据
*/
#pragma mark~~~~~~~~~字符串对象的
写入
和 读取
//第一步:创建字符串对象
NSString *shi =
@" 静夜思\n
床前明月光,\n
疑是地上霜,\n 举头望明月,\n
低头思故乡。";
//第二步:构造字符串存储路径
NSString *strPath = [document
stringByAppendingPathComponent:@"/text.txt"];
//第三部:将文件写入指定路径
[shi writeToFile:strPath
atomically:YES
encoding:NSUTF8StringEncoding
error:nil];
NSLog(@"strPath==%@",strPath);
//读取txt
NSString *readText=[NSString
stringWithContentsOfFile:strPath encoding:NSUTF8StringEncoding
error:nil];
NSLog(@"readText=%@",readText);
#pragma mark~~~~~~~~~~数组对象的
写入和
读取
//构造一个数组对象
NSMutableArray *starArray=[NSMutableArray
arrayWithObjects:@"Kitty",@"Tom"@"Eid",@"Jack",@"Ilice",
nil];
//第二步:构建数组存储路径,创建.plist文件
NSString *arrayPath=[document
stringByAppendingPathComponent:@"/array.plist"];
//将数组写入plist
[starArray writeToFile:arrayPath
atomically:YES];
NSLog(@"arrayPath=%@",arrayPath);
//数组中再添加新元素
[starArray addObject:@"baby"];
[starArray writeToFile:arrayPath
atomically:YES];
NSLog(@"arrayPath==%@",arrayPath);
//读取
NSArray *readArray=[NSArray
arrayWithContentsOfFile:arrayPath];
NSLog(@"%@",readArray);
#pragma mark ~~~~~~~~~~~~字典对象的写入
//构造一个字典
语法堂
前面是key 后面是value
NSDictionary *dic=@{@"name":@"Kitty",@"age":@"18",@"sex":@"girl",@"hobby":@[@"sleep",@"eat"]};
//第二步:构造存储字典的plist文件的存储路径
NSString *dicPath = [document
stringByAppendingPathComponent:@"dic.plist"];
//第三部:写入
[dic writeToFile:dicPath
atomically:YES];
NSLog(@"dicPath=%@",dicPath);
//第四步:读取
NSDictionary *readDic=[NSDictionary
dictionaryWithContentsOfFile:dicPath];
NSLog(@"readDic=%@",readDic);
#pragma mark~~~~~~~~NSData对象的写入和读取
//第一步:创建图片
UIImage *image = [UIImage
imageNamed:@"head_19.jpg"];
//第二步:将图片转化为二进制
NSData *imageData=UIImageJPEGRepresentation(image,
0.5);
//构造NSData文件.da的存储路径
NSString *imagePath=[document
stringByAppendingPathComponent:@"/data.da"];
//第四步:写入
[imageData writeToFile:imagePath
atomically:YES];
NSLog(@"imagePath=%@",imagePath);
//读取
UIImage *readImage=[UIImage
imageWithContentsOfFile:imagePath];
NSLog(@"%@",readImage);
相关文章推荐
- GUI - Web前端开发框架
- 评价ui设计作品好坏的八个标准(界面/交互设计研究)
- 为Yahoo! UI Extensions Grid增加内置的可编辑器
- Cocos2d-x UI开发之文本类使用实例
- 一些关于数据存储和查询优化的想法
- 谈谈对jquery ui tabs 的理解
- 在IE 浏览器中使用 jquery的fadeIn() 效果 英文字符字体加粗
- UI Events 用户界面事件
- jquery UI Datepicker时间控件的使用方法(基础版)
- 基于jQuery UI CSS Framework开发Widget的经验
- jquery ui 1.7 ui.tabs 动态添加与关闭(按钮关闭+双击关闭)
- jQuery UI设置固定日期选择特效代码分享
- Jquery ajax 同步阻塞引起的UI线程阻塞问题
- jQuery UI的Dialog无法提交问题的解决方法
- jQuery ui实现动感的圆角渐变网站导航菜单效果代码
- jquery UI Datepicker时间控件的使用方法(终结版)
- jquery UI Datepicker时间控件的使用方法(加强版)
- 关于jQuery UI 使用心得及技巧
- jQuery ui 1.7更新小结
- 从零开始学习jQuery (十) jQueryUI常用功能实战