iOS中UI界面 初级数据持久化

所谓的数据持久化,包括缓存以及永久性存储的我们访问过,或一直保留其存在;

下面是UIViewController中,整个的解析过程:解析中用到了导航视图控制器;

- (void)viewDidLoad {

    [super viewDidLoad];


    self.view.backgroundColor = [UIColor cyanColor];

    self.navigationItem.title = @"初级数据持久化";

#pragma mark -------------沙盒路径

    //获取沙盒路径(主路径)

    NSString *path = NSHomeDirectory();

      NSLog(@"沙盒路径:%@",path);

//    NSTemporaryDirectory() tmp 缓存

#pragma mark------------获取沙盒中子文件夹路径的方式 --- 拼接路径;

    //方式一 路径拼接获取文件夹;

    //获取document文件夹路径;

    NSString *document = [path stringByAppendingPathComponent:@"/Documents"];

    NSLog(@"沙盒路径:::%@",document);

    //获取Library文件夹路径;

    NSString *library = [path stringByAppendingPathComponent:@"/Library"];

    NSLog(@"沙盒路径====%@",library);

    NSString *tmp = [path stringByAppendingPathComponent:@"/tmp"];

    NSLog(@"沙盒路径====%@",tmp);

    

#pragma mark------------获取沙盒中子文件夹路径的方式 --- 函数路径;

    //获取documents的文件路径:/Users/sdzy7/Library/Developer/CoreSimulator/Devices/0AD931B4-EA1B-4120-9A34-1228B7F4EB79/data/Containers/Data/Application/A6CFC1DC-D569-497B-8A59-21ED1900F3C2/Documents

    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

    NSLog(@"documentPath = %@",documentPath);


    NSString *libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask,YES) lastObject];

    NSLog(@"libraryPath = %@",libraryPath);


    NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];

    NSLog(@"cachPath = %@",cachPath);


    NSString *tmPath = NSTemporaryDirectory();

    NSLog(@"tmPath = %@",tmPath);

    

    //应用程序路径:拿到素材,程序中用到的素材;

    NSString *pathe =[[NSBundle mainBundle] resourcePath];

    NSLog(@"%@",pathe);

#pragma mark------------简单文件的写入与读取;


    /*

     简单对象:NSString,NSSet,NSDictionary,NSArrayNSData

     数据持久化:把简单数据写入到文件中,存储到沙盒文件夹里

     

     NSData 用于存储二进制数据;

     

     */

#pragma mark-------------字符串数对象的写入和读取;

    //创建一个字符串;

    NSString *poetry = @"春江花月夜\n春江潮水连海平,海上明月共潮生\n滟滟随波千万里,何处春江无月明\n 江流宛转绕芳甸,月照花林皆似霰\n空里流霜不觉飞,汀上白沙看不见\n江天一色无纤尘,皎皎空中孤月轮\n江畔何人初见月?江月何年初照人?\n人生代代无穷已,江月年年望相似\n不知江月待何人,但见长江送流水\n白云一片去悠悠,青枫浦上不胜愁\n谁家今夜扁舟子?何处相思明月楼?\n可怜楼上月徘徊,应照离人妆镜台\n玉户帘中卷不去,捣衣砧上拂还来\n此时相望不相闻,愿逐月华流照君\n鸿雁长飞光不度,鱼龙潜跃水成文\n昨夜闲潭梦落花,可怜春半不还家\n江水流春去欲尽,江潭落月复西斜\n斜月沉沉藏海雾,碣石潇湘无限路\n不知乘月几人归,落月摇情满江树";

    NSString *strPath = [document stringByAppendingPathComponent:@"/text.text"];

    //将文件写入指定路ing

    [poetry writeToFile:strPath atomically:YES encoding:NSUTF8StringEncoding error:nil];

    NSLog(@"strPath = %@",strPath);

    //读取text

    NSString *readText = [NSString stringWithContentsOfFile:strPath encoding:NSUTF8StringEncoding error:nil];

    NSLog(@"%@",readText);

    

#pragma mark-------------数组对象的写入和读取;

    //写入;

    //创建一个数组对象;

    NSMutableArray *startArray = [NSMutableArray arrayWithObjects:@"春江花月夜",@"长恨歌",@"离骚",@"湘夫人",@"乐府诗句", nil];

    

    NSString *arrayPath = [document stringByAppendingPathComponent:@"array.plist"];

    [startArray writeToFile:arrayPath atomically:YES];

    NSLog(@"%@",arrayPath);

    

    //在数组中添加新元素;

    [startArray addObject:@"江城子·密州出猎"];

    [startArray writeToFile:arrayPath atomically:YES];

    

    NSArray *readArray = [NSArray arrayWithContentsOfFile:arrayPath];

    NSLog(@"%@",readArray);

    

#pragma mark-------------字典对象的写入和读取;

    //写入:

    //构造一个字典;

    NSDictionary *dic = @{@"乐府诗集":@"长恨歌",@"五言律诗":@"游子吟"};

    //构建dic.plist文件的构造路径

    NSString *dicPath = [document stringByAppendingPathComponent:@"dic.plist"];

    [dic writeToFile:dicPath atomically:YES];

    NSLog(@"%@",dicPath);

    NSDictionary *readdic = [NSDictionary dictionaryWithContentsOfFile:dicPath];

    NSLog(@"%@",readdic);

#pragma mark-------------NSData对象的写入和读取;

    //创建图片;

    UIImage *image = [UIImage imageNamed:@"head_01.jpg"];

    //将图片转化为二进制;

    NSData *imageDate = UIImageJPEGRepresentation(image, 0.5);

    //构造NSData.da的存储路径;

    NSString *imagePath = [document stringByAppendingString:@"/data.da"];

    //写入;

    [imageDate writeToFile:imagePath atomically:YES];

    

}

上述中,nslog 在控制台打印的是文档的途径,每次打印的结果都不相同,可以在电脑上根据地址找到相应的文档位置,我用的是Mac mini,做法是这样的:选中Finder -->右键-->前往文件夹 -->将控制台打印出的地址复制进去,就可以找到文件;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值