iOS入门(三十六) 初级数据持久化

初级数据持久化  
什么是沙盒机制
给一个空间,自由支配 
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 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值