iOS 用本地文件保存自定义模型
来源:互联网 发布:mysql数据库的安装步骤 编辑:程序博客网 时间:2024/06/06 07:11
iOS中,保存数据有四种方法,归档、文件、NSUserDefaults和sqlite数据库。每一种方式都有其特定的类型,在上一篇文章中介绍了用NSUserDefaults保存自定义模型的数据,这一篇来介绍一下用本地文件保存自定义模型的数据。
在自定义模型中,要遵守<NSCopying>协议
点h
#import <Foundation/Foundation.h>@interface ChatLogModel : NSObject<NSCopying>@property(nonatomic,copy)NSString *name;@property(nonatomic,assign)BOOL isVideo;@property(nonatomic,strong)NSString *date;@end
点m
#import "ChatLogModel.h"#define NAME @"name"#define ISVIDEO @"isVideo"#define DATE @"date"@implementation ChatLogModel- (void)encodeWithCoder:(NSCoder *)aCoder{ [aCoder encodeObject:self.name forKey:NAME]; [aCoder encodeBool:self.isVideo forKey:ISVIDEO]; [aCoder encodeObject:self.date forKey:DATE];}- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder{ if (self = [super init]) { self.name = [aDecoder decodeObjectForKey:NAME]; self.isVideo = [aDecoder decodeBoolForKey:ISVIDEO]; self.date = [aDecoder decodeObjectForKey:DATE]; } return self;}@end
在使用的时候
-(void)storeChatLogWithFile{// 获取路径 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"chatlog.plist"]; NSFileManager *fileM = [NSFileManager defaultManager];// 判断文件是否存在,不存在则直接创建,存在则直接取出文件中的内容 if (![fileM fileExistsAtPath:filePath]) { [fileM createFileAtPath:filePath contents:nil attributes:nil]; } NSMutableArray *chatLogArray = [NSMutableArray arrayWithContentsOfFile:filePath]; if ((chatLogArray.count == 0)) { chatLogArray = [NSMutableArray arrayWithCapacity:1]; } // 要保存的自定义模型 ChatLogModel *chatmodel = [[ChatLogModel alloc] init]; chatmodel.name = @"张三"; chatmodel.isVideo = YES;// 获取当前时间 NSDate *currentDate = [NSDate date]; NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"MM-dd hh:mm:ss"]; NSString *dateString = [formatter stringFromDate:currentDate]; chatmodel.date = dateString; [chatLogArray addObject:chatmodel];/* 这是正常的保存和取出数组内容到文件 存 [chatLogArray writeToFile:filePath atomically:YES]; 取 NSMutableArray *array = [NSMutableArray arrayWithContentsOfFile:filePath];*/ // 注意 数组中保存的是自定义模型,要想把数组保存在文件中,应该用下面的方法// 存 [NSKeyedArchiver archiveRootObject:chatLogArray toFile:filePath];// 取 NSArray *array = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath]; NSLog(@"array:%@",array);}
0 0
- iOS 用本地文件保存自定义模型
- iOS 用本地文件保存自定义模型
- iOS自定义对象保存到本地文件
- 自定义线程从网络获取图片资源并保存本地文件
- ios保存自定义对象
- iOS 本地文件
- 贴一段用javascript打开和保存本地文件的源码
- iOS开发-简单方法实现扒下网站html保存到本地文件
- [arcengine][栅格]保存为本地文件
- rdkafka 保存offset到本地文件
- Android保存Bitmap为本地文件
- C++保存数据到本地文件
- python保存数据到本地文件
- ios 使用NSUserDefaults保存自定义对象
- iOS - 保存自定义对象(姓名,性别.....)
- iOS 保存图片到自定义相册
- IOS写本地文件
- IOS --- 读取本地文件
- 【易语言界面开发系列教程之(EX_UI使用系列教程(11)--EX组件(单选框和复选框))】
- Spring的生命周期
- 在qemu中运行magenta
- Alter table alter |change |modify 区别
- An App ID with Identifier 'XXX.XXX.XXX’ is not available. Please enter a different string
- iOS 用本地文件保存自定义模型
- 使用webpack 使用Echarts3简单入门
- c#客户端Json转dataTable
- eclipse jni (C/C++)代码提示
- Python & PyCharm & Django 搭建web开发环境
- 使用FFmpeg实现的转码器
- 在Centos7下源码安装配置Redis
- Android加载大图Bitmap发生OOM(Out Of Memmory Error)解决方案
- Android 一起来封装一个简单易用的Adapter