您的位置:首页 > 移动开发 > IOS开发

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: