UI18_数据持久化

来源:互联网 发布:淘宝客服的流程 编辑:程序博客网 时间:2024/06/10 16:56

ViewController.h

#import <UIKit/UIKit.h>@interface ViewController : UIViewController@end

ViewController.m

#import "ViewController.h"#import "Student.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.//    NSLog(@"%@", NSHomeDirectory());    //  程序运行会分配一个沙盒文件, 沙盒文件的文件名每次运行都会变化, 文件夹名只一次有效, 沙盒一般会保存程序的一些信息, 缓存等数据, 还可以进行病毒的测试    //  沙盒里有三个文件夹: Documents, library, tmp    //  Documents文件夹: 保存用户想要的一些信息, 比如收藏的内容, 设置信息    //  library文件夹: 用来保存开发人员存储的一些东西    //  caches缓存文件夹: 用来对缓存信息进行存储, 清除缓存就是杀出该文件夹    //  Preferences用来保存一些信息, NSUserDefaults创建的plist文件保存在这个文件里    //  tmp临时文件夹: 存储临时文件    //  NSBundle指的是当前工程的文件夹, 在里面和可以获取图片的信息, 应用程序会在编译的时候把文件变成只读文件, 存入到NSBundle里//    //  1. 把简单的对象存入到本地//    //  简单对象��️: NSString, NSArray...//    NSString *str = @"永恒之光, 丝袜的骑士是平原的王者, 罗多克的军士带一块大门板, 极其抗揍, 库吉特的骑兵由于骑马与砍杀的骑兵AI不好所以显得很水, 萨兰德马穆鲁克很牛逼, 能量产, 升级快, 维吉亚的骑兵升级比较快, 兵种比较平衡~";//    //    //  1. 先找到目标文件夹的路径Documents//    //  参数1: 前往指定文件夹的名, NSDocumentDirectory是Documents用的, 64行, 注意不要和63行搞混. 还可以指定缓存等文件夹//    //  参数2: 文件夹的类型, 用户类型//    //  参数3: YES是绝对路径, 能找到整个路径, NO是相对路径, 只保留了前往的文件夹的名, 前面用~代替//    NSArray *sandBox = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);//    NSLog(@"%@", sandBox);//    ////    NSString *sandBoxPath = sandBox[0];//    //  把要保存的内容进行路径的拼接//    NSString *docPath = [sandBoxPath stringByAppendingString:@"/骑马与砍杀.xml"];//    //  把字符串写入本地//    //  参数1: 要写入的文件路径//    //  参数2: 是否保护正在写入的文件//    //  参数3: 编码格式//    [str writeToFile:docPath atomically:YES encoding:NSUTF8StringEncoding error:nil];//    NSLog(@"%@", docPath);//    //  把数组对象写入到本地//    NSArray *arr = @[@"萨兰德", @"维吉亚", @"库吉特", @"罗多克", @"斯瓦迪亚", @"卡拉迪亚"];//    //    NSArray *sandBox = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 1, YES);//    NSString *sandBoxPath = sandBox[0];//    //  路径拼接//    NSString *arrPath = [sandBoxPath stringByAppendingPathComponent:@"卡拉迪亚大陆.xml"];//    //  按照指定的文件路径, 数据写入到本地//    [arr writeToFile:arrPath atomically:YES];//    NSLog(@"%@", arrPath);//    //    //  从本地把数据读出来//    NSArray *receiveArr = [NSArray arrayWithContentsOfFile:arrPath];//    for (NSString *str in receiveArr) {//        NSLog(@"%@", str);//    }//    //    //  1. 指定文件保存的沙盒文件路径//    //  2. 拼接要保存的文件路径//    //  3. 根据路径, 把数据进行写入//    //  4. 按照文件路径, 从本地再读取数据//    //    // 写一个字典, 然后参照数组的方法, 把字典写入到本地//    //  从本地把字典读出来//    NSDictionary *dic = @{@"精灵": @"阿尔达利亚",//                          @"蓝国游侠": @"射术无双",//                          @"红国禁卫": @"打遍天下",//                          @"绿国大军": @"斧头帮在此",//                          @"黄色帝国": @"各种标枪"};//    NSArray *dicSandBox = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 1, YES);//    NSString *dicSandBoxPath = dicSandBox[0];//    NSString *dicPath = [dicSandBoxPath stringByAppendingString:@"列国兵种.plist"];//    [dic writeToFile:dicPath atomically:YES];//    NSLog(@"dic: %@", dicPath);//    //  读取//    NSDictionary *recevieDic = [NSDictionary dictionaryWithContentsOfFile:dicPath];//    NSLog(@"%@", recevieDic);//    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];//    [user setObject:@"伊斯兰迪尔" forKey:@"非人精灵"];//    NSLog(@"%@", NSHomeDirectory());//    //  从本地读字符串//    NSString *str = [NSString stringWithContentsOfFile:<#(NSString *)#> encoding:<#(NSStringEncoding)#> error:<#(NSError **)#>];//    //  复杂对象, 写入到本地//    Student *stu = [[Student alloc] init];//    stu.name = @"伊斯兰迪尔";//    stu.hobby = @"精灵美酒";//    stu.number = @100;//    stu.age = 33;//    //  归档操作//    //  找沙盒路径//    NSArray *sandBox = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 1, YES);//    NSString *sandBoxPath = sandBox[0];//    NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"精灵.avi"];//    NSLog(@"%@", docPath);//    [NSKeyedArchiver archiveRootObject:stu toFile:docPath];//    //  反归档: 从本地再读出来//    Student *tempStu = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];//    NSLog(@"%@, %@", tempStu.name, tempStu.number);////    //  把复杂对象放到数组里进行归档和反归档的操作//    Student *stu1 = [[Student alloc] initWithName:@"紫玫" hobby:@"玫瑰" age:18 number:@126];//    Student *stu2 = [[Student alloc] initWithName:@"雪芍" hobby:@"芍药" age:17 number:@109];//    Student *stu3 = [[Student alloc] initWithName:@"玉兰" hobby:@"玉兰" age:19 number:@187];//    Student *stu4 = [[Student alloc] initWithName:@"月桂" hobby:@"雨夜" age:21 number:@137];//    NSArray *arr = @[stu1, stu2, stu3, stu4];//    //  把复杂对象保存到数组中, 然后整体进行归档和反归档的操作//    //  NSArray因为本身就签署了NSCoding协议, 所以可以直接进行归档和反归档操作, 自己写的复杂类也必须签NSCoding协议实现方法, 否则就会崩溃//    //    //  1. 找路径//    NSArray *sandBox = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);//    NSString *sandBoxPath = sandBox[0];//    //  2. 给文件拼接路径//    NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"student.plist"];//    //  3. 归档或者写入//    [NSKeyedArchiver archiveRootObject:arr toFile:docPath];//    NSLog(@"%@", docPath);//    //    //  反归档//    NSArray *tempArr = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];//    for (Student *stu in tempArr) {//        NSLog(@"%@", stu.name);//    }    //  文件管理    //  1. 找到沙盒路径    NSArray *sandBox = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);    NSString *sandBOxPath = sandBox[0];    //  创建一个文件管理者    NSFileManager *manager = [NSFileManager defaultManager];    //  要拼接一个文件夹的路径    //  文件夹的名, 没有扩展名    NSString *docPath = [sandBOxPath stringByAppendingPathComponent:@"tianqin"];    //  根据路径创建一个文件夹    [manager createDirectoryAtPath:docPath withIntermediateDirectories:YES attributes:nil error:nil];    NSLog(@"%@", docPath);    //  在新创建的文件夹里写入一个txt文本的字符串    NSString *str = @"天琴斩月";    NSString *newPath = [docPath stringByAppendingPathComponent:@"test.txt"];    [str writeToFile:newPath atomically:YES encoding:NSUTF8StringEncoding error:nil];    //  删除缓存文件    NSArray *cacheBox = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);    NSString *cacheBoxPath = cacheBox[0];    //  删除文件夹,文件    BOOL result = [manager removeItemAtPath:cacheBoxPath error:nil];    if (result) {        NSLog(@"删除成功");    } else {        NSLog(@"删除失败");    }}

Student.h

#import <Foundation/Foundation.h>//  让复杂类写入到本地执行归档和反归档的操作, 只有类签订NSCoding的协议之后, 实现协议方法才能进行归档反归档的操作@interface Student : NSObject<NSCoding>@property(nonatomic, copy)NSString *name;@property(nonatomic, copy)NSString *hobby;@property(nonatomic, assign)NSInteger age;@property(nonatomic, retain)NSNumber *number;- (instancetype)initWithName:(NSString *)name hobby:(NSString *)hobby age:(NSInteger)age number:(NSNumber *)number;@end

Student.m

#import "Student.h"@implementation Student- (void)encodeWithCoder:(NSCoder *)aCoder;{    //  对数据进行编码操作    //  把需要归档的数据进行编码操作, 不需要的就不写了    [aCoder encodeObject:self.name forKey:@"name"];    [aCoder encodeObject:self.hobby forKey:@"hobby"];    [aCoder encodeObject:self.number forKey:@"number"];    [aCoder encodeInteger:self.age forKey:@"age"];}- (id)initWithCoder:(NSCoder *)aDecoder {    self = [super init];    if (self) {        //  解码操作        //  把解码之后的值赋值给属性        self.name = [aDecoder decodeObjectForKey:@"name"];        self.hobby = [aDecoder decodeObjectForKey:@"hobby"];        self.number = [aDecoder decodeObjectForKey:@"number"];        self.age = [aDecoder decodeIntegerForKey:@"age"];    }    return self;}- (instancetype)initWithName:(NSString *)name hobby:(NSString *)hobby age:(NSInteger)age number:(NSNumber *)number {    self = [super init];    if (self) {        self.name = name;        self.hobby = hobby;        self.age = age;        self.number = number;    }    return self;}@end
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 买的家具味道大怎么办 买的木床有味道怎么办 宝宝拍胸片衣服有扣子怎么办 前扣内衣扣子坏了怎么办 运动内衣穿上平胸怎么办 文胸的海绵变形了怎么办 文胸背部松紧变形了怎么办 黑色裤子被漂白水漂红了怎么办 黑色裤子晒红了怎么办 黑色裤子泛红了怎么办 黑色裤子洗红了怎么办 新内衣穿了痒怎么办 灰色针织运动裤洗完发黄怎么办 皮衣买大了一号怎么办 紧身衣破了一个洞怎么办 紧身衣破了一个小洞怎么办 内衣肩带不带调整的怎么办 不正确佩戴胸罩导致下垂怎么办 增肌时期晚上饿怎么办 啤酒和可乐喝了怎么办 脸部毛孔堵塞不出汗怎么办 减肥运动完饿了怎么办 想减肥晚上饿了怎么办 孕妇喝不进去水怎么办 跑步喝多了水怎么办 衣柜门轮子坏了怎么办 穿瑜伽裤下边有缝怎么办 3岁宝宝比较内向怎么办 我想和我同学搞基怎么办 自己做的葡萄酒太甜怎么办 吉米学校想退款怎么办 汽车租赁公司不退押金怎么办 身材不好怎么办健身教练形象照 反祈祷式做不到怎么办 窦性心跳过缓怎么办 四维彩超查出胎儿心脏有问题怎么办 减脂肚子饿了怎么办 做瑜伽腰扭到了怎么办 出了汗怎么办活动反思 农村都是老人和孩子怎么办 瑜伽垫容易出现痕迹怎么办