数据存储之归档

在使用plist进行数据存储和读取,只适用于系统自带的一些常用类型才能用,且必须先获取路径相对麻烦;
偏好设置(将所有的东西都保存在同一个文件夹下面,且主要用于存储应用的设置信息)
归档:因为前两者都有一个致命的缺陷,只能存储常用的类型。归档可以实现把自定义的对象存放在文件中。
代码演示
先看下项目的结构  Student类继承Person类
在ViewController.m中
//
//  ViewController.m
//  Archive
//
//  Created by City--Online on 15/4/22.
//  Copyright (c) 2015年 CYW. All rights reserved.
//

#import "ViewController.h"
#import "Student.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSArray *documents=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentpath=[documents firstObject];
//    NSLog(@"%@",documentpath);
    NSString *filePath=[documentpath stringByAppendingPathComponent:@"CYW"];
    
//使用archiveRootObject简单归档 单个对象
    //缺点:只能把一个对象归档到一个文件
#if 0
    NSArray *arr=[[NSArray alloc]initWithObjects:@"1",@"2",@"3", nil];
    //归档 //可对字典、数组、字符串、数字等进行归档 返回bool值表示是否归档成功
    [NSKeyedArchiver archiveRootObject:arr toFile:filePath];
#endif
#if 0
    //解挡
    NSArray *arr=[NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
    NSLog(@"%@",arr);
#endif
 
     //多个对象归档
#if 0
    //准备数据
    CGPoint point=CGPointMake(100, 100);
    NSString *mystring=@"cuiyanwei";
    BOOL YesOrNo=YES;
    //归档
    NSMutableData *mutableData=[[NSMutableData alloc]init];
    NSKeyedArchiver *archiver=[[NSKeyedArchiver alloc]initForWritingWithMutableData:mutableData];
    
    [archiver encodeCGPoint:point forKey:@"mypoint"];
    [archiver encodeBool:YesOrNo forKey:@"mybool"];
    [archiver encodeObject:mystring forKey:@"mystring"];
    [archiver finishEncoding];
    [mutableData writeToFile:filePath atomically:YES];
#endif
#if 0
    //多对象解档
    NSMutableData *mutableData=[[NSMutableData alloc]initWithContentsOfFile:filePath];
    NSKeyedUnarchiver *unarchiver=[[NSKeyedUnarchiver alloc]initForReadingWithData:mutableData];
    CGPoint point=[unarchiver decodeCGPointForKey:@"mypoint"];
    BOOL mybool=[unarchiver decodeBoolForKey:@"mybool"];
    NSString *mystring=[unarchiver decodeObjectForKey:@"mystring"];
    [unarchiver finishDecoding];
    NSLog(@"X=%lf,Y=%lf\n mybool=%d\n mystring=%@",point.x,point.y,mybool,mystring);
    UIImage
#endif
    
#if 0
    //自定义对象归档
    Student *stu1=[[Student alloc]init];
    stu1.name=@"cuiyanwei";
    stu1.age=24;
    stu1.photo=[UIImage imageNamed:@"email.png"];
    stu1.stuId=@"001";
    
    Student *stu2=[[Student alloc]init];
    stu2.name=@"xiaocui";
    stu2.age=23;
    stu2.photo=[UIImage imageNamed:@"email.png"];
    stu2.stuId=@"002";
    
    //归档
    NSArray *array=[[NSArray alloc]initWithObjects:stu1,stu2, nil];
    NSMutableData *mutableData=[[NSMutableData alloc]init];
    NSKeyedArchiver *archiver=[[NSKeyedArchiver alloc]initForWritingWithMutableData:mutableData];
    [archiver encodeObject:array forKey:@"students"];
    [archiver finishEncoding];
    [mutableData writeToFile:filePath atomically:YES];
#endif
#if 1
    //解档
    NSMutableData *mutableData=[[NSMutableData alloc]initWithContentsOfFile:filePath];
    NSKeyedUnarchiver *unarchiver=[[NSKeyedUnarchiver alloc]initForReadingWithData:mutableData];
    NSArray *array=[unarchiver decodeObjectForKey:@"students"];
    for (Student *stu in array) {
        NSLog(@"name=%@ age=%ld photo=%@ stuId=%@",stu.name,stu.age,stu.photo,stu.stuId);
    }
#endif

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

在Person.h中定义了几个属性

//
//  Person.h
//  Archive
//
//  Created by City--Online on 15/4/22.
//  Copyright (c) 2015年 CYW. All rights reserved.
//

#import <Foundation/Foundation.h>
//头像要添加框架在类中
#import <UIKit/UIKit.h>

//自定义保存到文件需要实现NSCoding协议
@interface Person : NSObject<NSCoding>

@property(nonatomic,strong)NSString *name;
@property(nonatomic,assign)NSInteger age;
@property(nonatomic,strong)UIImage *photo;

@end

 在Person.m中实现NSCoding协议

//
//  Person.m
//  Archive
//
//  Created by City--Online on 15/4/22.
//  Copyright (c) 2015年 CYW. All rights reserved.
//

#import "Person.h"


@implementation Person

- (void)encodeWithCoder:(NSCoder *)aCoder
{
     NSLog(@"person encodeWithCoder");
    [aCoder encodeObject:self.name forKey:@"name"];
    [aCoder encodeInteger:self.age forKey:@"age"];
    [aCoder encodeObject:self.photo forKey:@"photo"];
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
    if (self=[super init]) {
         NSLog(@"person encodeWithCoder");
        _age=[aDecoder decodeIntegerForKey:@"age"];
        _name=[aDecoder decodeObjectForKey:@"name"];
        _photo=[aDecoder decodeObjectForKey:@"photo"];
    }
    return self;
}
@end

 在Student类中继承Person类 增加了一个学号属性

#import "Person.h"

@interface Student : Person
@property(nonatomic,strong)NSString *stuId;
@end

 在Student.m中实现了NSCodeing协议

//
//  Student.m
//  Archive
//
//  Created by City--Online on 15/4/22.
//  Copyright (c) 2015年 CYW. All rights reserved.
//

#import "Student.h"

@implementation Student
- (void)encodeWithCoder:(NSCoder *)aCoder
{
    [super encodeWithCoder:aCoder];
    NSLog(@"student encodeWithCoder");
    [aCoder encodeObject:self.stuId forKey:@"stuId"];
    
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
    if (self=[super initWithCoder:aDecoder]) {
        NSLog(@"student initWithCoder");
        _stuId=[aDecoder decodeObjectForKey:@"stuId"];
    }
    return self;
}
@end

 自定义模型对象归档解档运行结果如下:

主要是实现NSCoding协议

 

 
 
 
内容概要:本文深入解析了扣子COZE AI编程及其详细应用代码案例,旨在帮助读者理解新一代低门槛智能体开发范式。文章从五个维度展开:关键概念、核心技巧、典型应用场景、详细代码案例分析以及未来发展趋势。首先介绍了扣子COZE的核心概念,如Bot、Workflow、Plugin、Memory和Knowledge。接着分享了意图识别、函数调用链、动态Prompt、渐进式发布及监控可观测等核心技巧。然后列举了企业内部智能客服、电商导购助手、教育领域AI助教和金融行业合规质检等应用场景。最后,通过构建“会议纪要智能助手”的详细代码案例,展示了从需求描述、技术方案、Workflow节点拆解到调试与上线的全过程,并展望了多智能体协作、本地私有部署、Agent2Agent协议、边缘计算插件和实时RAG等未来发展方向。; 适合人群:对AI编程感兴趣的开发者,尤其是希望快速落地AI产品的技术人员。; 使用场景及目标:①学习如何使用扣子COZE构建生产级智能体;②掌握智能体实例、自动化流程、扩展能力和知识库的使用方法;③通过实际案例理解如何实现会议纪要智能助手的功能,包括触发器设置、下载节点、LLM节点Prompt设计、Code节点处理和邮件节点配置。; 阅读建议:本文不仅提供了理论知识,还包含了详细的代码案例,建议读者结合实际业务需求进行实践,逐步掌握扣子COZE的各项功能,并关注其未来的发展趋势。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值