UI18_数据持久化

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Node-RED是一个开源的可视化编程工具,用于构建物联网应用和流程。它提供了丰富的节点来处理、转换和可视化数据。 在Node-RED,可以使用“uitable”节点来创建和管理一个持久化数据表格。uitable节点基于本地文件系统,将数据保存在磁盘上,以便在以后的运行可以重用。 要使用uitable节点进行持久化,需要先安装uitable依赖。可以通过Node-RED的命令行界面(CLI)或通过编辑settings.js文件来安装依赖。安装完成后,可以在Flows的节点面板找到uitable节点。 将uitable节点拖放到工作区,并与其他节点连接。然后,可以配置uitable节点的属性,包括数据存储位置、表格名称和列名。可以将输入的数据连接到uitable节点,以将数据存储到表格。 当启动或重新加载Flow时,uitable节点会自动加载之前保存的数据,并将其添加到表格。这样,即使重新启动Node-RED,也能够保留之前保存的数据。 在Flow可以使用uitable节点的输出来读取和处理已保存的数据。可以使用其他节点,如ui_table、debug等节点来实时显示和分析表格数据。 需要注意的是,uitable节点会将数据以JSON格式进行存储,因此可以灵活地对表格进行处理和修改。 总结起来,Node-RED的uitable节点提供了一种简单可视化的方式来持久化数据表格。通过配置uitable节点的属性和连接输入输出节点,可以方便地存储和读取数据,并在应用程序进行处理和可视化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值