UI中初级数据存储

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);

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值