iOS入门(三十六) 初级数据持久化
2015-08-14 15:06
399 查看
初级数据持久化
什么是沙盒机制
给一个空间,自由支配
preferences 偏好设置
BSBundle .4
两种产生UIImage的途径
UIImage * image = [UIImage imageWithContentsOfFile:imagePath];
1、稳定 2、 直接从文件读取(文件-内存)单向,只有一次(背景图片)
使用UIImage imageNamed:
1、不稳定 2、 适合多次使用(可以重复使用,一直存在于内存)(图标)
绝对路径 相对路径
简单对象写入文件
//1、参数1:要搜索那个文件夹(Document文件夹)
//2、参数2:获得iphone用户文件件 (固定为NSUserDomainMask 枚举值中后面三个市为mac使用)
NSArray * arr =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask , YES);
// NSLog(@"%@",arr);
NSString * path = [arr objectAtIndex:0];
NSLog(@"%@",path);
// //获得NSBundle包得路径
// NSString * bundle = [NSBundle mainBundle] .resourcePath;
// NSLog(@"%@",bundle);
// //获取bundle包中某个文件的路径
// NSString * imagePath = [[NSBundle mainBundle] pathForResource:@"5" ofType:@"jpg"];
// NSLog(@"%@",imagePath);
// //通过文件路径产生一个UIImage
// UIImage * image = [UIImage imageWithContentsOfFile:imagePath];
//1、把字符串对象 写入Document文件夹内
//第一步,给要写入的字符串指定一个文件路径
NSString * txtPath = [path stringByAppendingString:@"/sss.txt"];
//第二步,创建一个字符串
NSString * str = @"很大很大hi捡垃圾低价出卡机";
//第三步,将字符串写入路径
[str writeToFile:txtPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
//数组写入路径(plist 或者xml)
NSString * arrPath = [path stringByAppendingString:@"/arr.xml"];
NSArray * arrr = [NSArray arrayWithObjects:@"的骄傲和杜威和",@"获得艾迪",@"fjiasd”", nil];
[arrr writeToFile:arrPath atomically:YES];
//字典写入路径
NSString * dicPath = [path stringByAppendingPathComponent:@"dic.plist"];
NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys:@"和嗲话",@"地哦我活动",@"单位ihdi",@"东海U盾",@"hdiuhd“",@"大河网iudh", nil];
[dic writeToFile:dicPath atomically:YES];
//data 写入沙盒
// int index = 0 ;
// NSData * data = [[NSData alloc] initWithBytes:&index length:sizeof(index)];
// NSString * dataPath = [path stringByAppendingPathComponent:@"hsdh.jds"];
// [data writeToFile:dataPath atomically:YES];
NSString * mp3Path = [[NSBundle mainBundle]pathForResource:@"01 在夜里" ofType:@"mp3"];
NSData * dataMP3 = [NSData dataWithContentsOfFile:mp3Path];
NSString *dataPath = [path stringByAppendingPathComponent:@"data.mp3"];
[dataMP3 writeToFile:dataPath atomically:YES];
复杂对象写入文件
复杂对象至少包含一个实例类
归档(封印)
NSArray * arr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSLog(@"%@",arr);
NSString * path = [arr objectAtIndex:0];
//封印地点
NSString * bossPath = [path stringByAppendingPathComponent:@"boss.sda"];
Boss * boss = [[Boss alloc]init];
boss.name = @"蒋神";
boss.job = @"魔王";
//把复杂对象首先要转成NSData 类型
//归档工具类,将id类型转换为NSData(将复杂对象进行序列化并且保存到文件路径下)
BOOL a = [NSKeyedArchiver archiveRootObject:boss toFile:bossPath];
NSLog(@"%d",a);
//反归档
Boss * bossre = [NSKeyedUnarchiver unarchiveObjectWithFile:bossPath];
NSLog(@"%@",bossre.name);
NSArray * array = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString * pathh = [array objectAtIndex:0];
NSString * studentPath = [pathh stringByAppendingPathComponent:@"student.jkh"];
Student * stu = [[Student alloc]init];
stu.name = @"付莉莉";
stu.grade = @"大四";
stu.sex = @"女";
BOOL b = [NSKeyedArchiver archiveRootObject:stu toFile:studentPath];
NSLog(@"%d",b);
Student * sture = [NSKeyedUnarchiver unarchiveObjectWithFile:studentPath];
NSLog(@"%@",sture.name);
@interface Boss: NSObject
@property(nonatomic , retain) NSString * name;
@property ( nonatomic , retain ) NSString * job;
@implementation Boss
//封印(归档) 的方法,将复杂对象转化成数据流(NSData) 的协议方法
- (void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:self.name forKey:@"name"];
[aCoder encodeObject:self.job forKey:@"job"];
}
//接触封印的方法,将数据流恢复成复杂对象
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [ super init];
if (self ) {
self.name = [aDecoder decodeObjectForKey:@"name"];
self.job = [aDecoder decodeObjectForKey:@"job"];
}
return self;
}
@end
//用于判断是不是第一次登陆
NSString * str = [[NSUserDefaults standardUserDefaults]objectForKey:@"first"];
NSLog(@"存储的数据 : %@",str);
//设置
[[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"first"];
//强制保存
[[NSUserDefaults standardUserDefaults]synchronize];
// NSUserDefaults :系统自带的plist 文件,用于第一次登陆,单例传值
//文件管理器
// NSFileManager
什么是沙盒机制
给一个空间,自由支配
preferences 偏好设置
BSBundle .4
两种产生UIImage的途径
UIImage * image = [UIImage imageWithContentsOfFile:imagePath];
1、稳定 2、 直接从文件读取(文件-内存)单向,只有一次(背景图片)
使用UIImage imageNamed:
1、不稳定 2、 适合多次使用(可以重复使用,一直存在于内存)(图标)
绝对路径 相对路径
简单对象写入文件
//1、参数1:要搜索那个文件夹(Document文件夹)
//2、参数2:获得iphone用户文件件 (固定为NSUserDomainMask 枚举值中后面三个市为mac使用)
NSArray * arr =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask , YES);
// NSLog(@"%@",arr);
NSString * path = [arr objectAtIndex:0];
NSLog(@"%@",path);
// //获得NSBundle包得路径
// NSString * bundle = [NSBundle mainBundle] .resourcePath;
// NSLog(@"%@",bundle);
// //获取bundle包中某个文件的路径
// NSString * imagePath = [[NSBundle mainBundle] pathForResource:@"5" ofType:@"jpg"];
// NSLog(@"%@",imagePath);
// //通过文件路径产生一个UIImage
// UIImage * image = [UIImage imageWithContentsOfFile:imagePath];
//1、把字符串对象 写入Document文件夹内
//第一步,给要写入的字符串指定一个文件路径
NSString * txtPath = [path stringByAppendingString:@"/sss.txt"];
//第二步,创建一个字符串
NSString * str = @"很大很大hi捡垃圾低价出卡机";
//第三步,将字符串写入路径
[str writeToFile:txtPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
//数组写入路径(plist 或者xml)
NSString * arrPath = [path stringByAppendingString:@"/arr.xml"];
NSArray * arrr = [NSArray arrayWithObjects:@"的骄傲和杜威和",@"获得艾迪",@"fjiasd”", nil];
[arrr writeToFile:arrPath atomically:YES];
//字典写入路径
NSString * dicPath = [path stringByAppendingPathComponent:@"dic.plist"];
NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys:@"和嗲话",@"地哦我活动",@"单位ihdi",@"东海U盾",@"hdiuhd“",@"大河网iudh", nil];
[dic writeToFile:dicPath atomically:YES];
//data 写入沙盒
// int index = 0 ;
// NSData * data = [[NSData alloc] initWithBytes:&index length:sizeof(index)];
// NSString * dataPath = [path stringByAppendingPathComponent:@"hsdh.jds"];
// [data writeToFile:dataPath atomically:YES];
NSString * mp3Path = [[NSBundle mainBundle]pathForResource:@"01 在夜里" ofType:@"mp3"];
NSData * dataMP3 = [NSData dataWithContentsOfFile:mp3Path];
NSString *dataPath = [path stringByAppendingPathComponent:@"data.mp3"];
[dataMP3 writeToFile:dataPath atomically:YES];
复杂对象写入文件
复杂对象至少包含一个实例类
归档(封印)
NSArray * arr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSLog(@"%@",arr);
NSString * path = [arr objectAtIndex:0];
//封印地点
NSString * bossPath = [path stringByAppendingPathComponent:@"boss.sda"];
Boss * boss = [[Boss alloc]init];
boss.name = @"蒋神";
boss.job = @"魔王";
//把复杂对象首先要转成NSData 类型
//归档工具类,将id类型转换为NSData(将复杂对象进行序列化并且保存到文件路径下)
BOOL a = [NSKeyedArchiver archiveRootObject:boss toFile:bossPath];
NSLog(@"%d",a);
//反归档
Boss * bossre = [NSKeyedUnarchiver unarchiveObjectWithFile:bossPath];
NSLog(@"%@",bossre.name);
NSArray * array = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString * pathh = [array objectAtIndex:0];
NSString * studentPath = [pathh stringByAppendingPathComponent:@"student.jkh"];
Student * stu = [[Student alloc]init];
stu.name = @"付莉莉";
stu.grade = @"大四";
stu.sex = @"女";
BOOL b = [NSKeyedArchiver archiveRootObject:stu toFile:studentPath];
NSLog(@"%d",b);
Student * sture = [NSKeyedUnarchiver unarchiveObjectWithFile:studentPath];
NSLog(@"%@",sture.name);
@interface Boss: NSObject
@property(nonatomic , retain) NSString * name;
@property ( nonatomic , retain ) NSString * job;
@implementation Boss
//封印(归档) 的方法,将复杂对象转化成数据流(NSData) 的协议方法
- (void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:self.name forKey:@"name"];
[aCoder encodeObject:self.job forKey:@"job"];
}
//接触封印的方法,将数据流恢复成复杂对象
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [ super init];
if (self ) {
self.name = [aDecoder decodeObjectForKey:@"name"];
self.job = [aDecoder decodeObjectForKey:@"job"];
}
return self;
}
@end
//用于判断是不是第一次登陆
NSString * str = [[NSUserDefaults standardUserDefaults]objectForKey:@"first"];
NSLog(@"存储的数据 : %@",str);
//设置
[[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"first"];
//强制保存
[[NSUserDefaults standardUserDefaults]synchronize];
// NSUserDefaults :系统自带的plist 文件,用于第一次登陆,单例传值
//文件管理器
// NSFileManager
相关文章推荐
- 【iOS开发】设置textView的默认提示文字,也就是textView变相的"placeholder"。
- iOS 大头针怎么固定在中间,且移动后及时更新位置信息
- (链接)xcode7,ios9 部分兼容设置
- 使用CocoaPods 出错
- iOS启动页
- iOS 地图移动中心点获取
- iOS应用程序生命周期(前后台切换,应用的各种状态)详解
- iOS中tableview通过点击实现Cell的伸缩
- IOS问题
- IOStime处理
- ios中将view转为image
- 【IOS】ios8推送消息注册
- IOS AFNetWorking 下载文件 断点续传
- IOS8 设置TableView Separatorinset 分割线从边框顶端开始
- IOS常用第三方框架 - Rosie
- 使用CocoaPods 但找不到头文件解决方法
- iOS中assign、copy 、retain等关键字的含义
- iOS中手机拍照的图片旋转
- iOS中的NSString引用计数问题(-1和整数最大值)
- iOS开发之监听键盘高度的变化