iOS 用本地文件保存自定义模型

iOS 用本地文件保存自定义模型 

标签: 自定义模型文件存储iosNSKeyedArchiver
  2163人阅读  评论(0)  收藏  举报
  分类:


iOS中,保存数据有四种方法,归档、文件、NSUserDefaults和sqlite数据库。每一种方式都有其特定的类型,在上一篇文章中介绍了用NSUserDefaults保存自定义模型的数据,这一篇来介绍一下用本地文件保存自定义模型的数据。


在自定义模型中,要遵守<NSCopying>协议

点h

[objc]  view plain  copy
  1. #import <Foundation/Foundation.h>  
  2.   
  3. @interface ChatLogModel : NSObject<NSCopying>  
  4.   
  5. @property(nonatomic,copy)NSString *name;  
  6. @property(nonatomic,assign)BOOL isVideo;  
  7. @property(nonatomic,strong)NSString *date;  
  8.   
  9. @end  

点m

[objc]  view plain  copy
  1. #import "ChatLogModel.h"  
  2.   
  3. #define NAME @"name"  
  4. #define ISVIDEO @"isVideo"  
  5. #define DATE @"date"  
  6.   
  7. @implementation ChatLogModel  
  8.   
  9. - (void)encodeWithCoder:(NSCoder *)aCoder  
  10. {  
  11.   
  12.     [aCoder encodeObject:self.name forKey:NAME];  
  13.     [aCoder encodeBool:self.isVideo forKey:ISVIDEO];  
  14.     [aCoder encodeObject:self.date forKey:DATE];  
  15. }  
  16. - (nullable instancetype)initWithCoder:(NSCoder *)aDecoder  
  17. {  
  18.     if (self = [super init]) {  
  19.         self.name = [aDecoder decodeObjectForKey:NAME];  
  20.         self.isVideo = [aDecoder decodeBoolForKey:ISVIDEO];  
  21.         self.date = [aDecoder decodeObjectForKey:DATE];  
  22.           
  23.     }  
  24.       
  25.     return self;  
  26. }  
  27.   
  28. @end  


在使用的时候

[objc]  view plain  copy
  1. -(void)storeChatLogWithFile  
  2. {  
  3. //    获取路径  
  4.     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
  5.     NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"chatlog.plist"];  
  6.     NSFileManager *fileM = [NSFileManager defaultManager];  
  7. //    判断文件是否存在,不存在则直接创建,存在则直接取出文件中的内容  
  8.     if (![fileM fileExistsAtPath:filePath]) {  
  9.         [fileM createFileAtPath:filePath contents:nil attributes:nil];  
  10.     }  
  11.     NSMutableArray *chatLogArray = [NSMutableArray arrayWithContentsOfFile:filePath];  
  12.     if ((chatLogArray.count == 0)) {  
  13.         chatLogArray = [NSMutableArray arrayWithCapacity:1];  
  14.     }  
  15.       
  16. //    要保存的自定义模型  
  17.     ChatLogModel *chatmodel = [[ChatLogModel alloc] init];  
  18.     chatmodel.name = @"张三";  
  19.     chatmodel.isVideo = YES;  
  20. //    获取当前时间  
  21.     NSDate *currentDate = [NSDate date];  
  22.     NSDateFormatter *formatter = [[NSDateFormatter alloc] init];  
  23.     [formatter setDateFormat:@"MM-dd hh:mm:ss"];  
  24.     NSString *dateString = [formatter stringFromDate:currentDate];  
  25.     chatmodel.date = dateString;  
  26.    
  27.     [chatLogArray addObject:chatmodel];  
  28.   
  29. /* 
  30.     这是正常的保存和取出数组内容到文件 
  31.     存 
  32.     [chatLogArray writeToFile:filePath atomically:YES]; 
  33.     取 
  34.     NSMutableArray *array = [NSMutableArray arrayWithContentsOfFile:filePath]; 
  35. */  
  36.    
  37.       
  38. //    注意 数组中保存的是自定义模型,要想把数组保存在文件中,应该用下面的方法  
  39. //    存  
  40.     [NSKeyedArchiver archiveRootObject:chatLogArray toFile:filePath];  
  41. //    取  
  42.     NSArray *array = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];  
  43.     NSLog(@"array:%@",array);  
  44.   
  45. }  


iOS中,保存数据有四种方法,归档、文件、NSUserDefaults和sqlite数据库。每一种方式都有其特定的类型,在上一篇文章中介绍了用NSUserDefaults保存自定义模型的数据,这一篇来介绍一下用本地文件保存自定义模型的数据。


在自定义模型中,要遵守<NSCopying>协议

点h

[objc]  view plain  copy
  1. #import <Foundation/Foundation.h>  
  2.   
  3. @interface ChatLogModel : NSObject<NSCopying>  
  4.   
  5. @property(nonatomic,copy)NSString *name;  
  6. @property(nonatomic,assign)BOOL isVideo;  
  7. @property(nonatomic,strong)NSString *date;  
  8.   
  9. @end  

点m

[objc]  view plain  copy
  1. #import "ChatLogModel.h"  
  2.   
  3. #define NAME @"name"  
  4. #define ISVIDEO @"isVideo"  
  5. #define DATE @"date"  
  6.   
  7. @implementation ChatLogModel  
  8.   
  9. - (void)encodeWithCoder:(NSCoder *)aCoder  
  10. {  
  11.   
  12.     [aCoder encodeObject:self.name forKey:NAME];  
  13.     [aCoder encodeBool:self.isVideo forKey:ISVIDEO];  
  14.     [aCoder encodeObject:self.date forKey:DATE];  
  15. }  
  16. - (nullable instancetype)initWithCoder:(NSCoder *)aDecoder  
  17. {  
  18.     if (self = [super init]) {  
  19.         self.name = [aDecoder decodeObjectForKey:NAME];  
  20.         self.isVideo = [aDecoder decodeBoolForKey:ISVIDEO];  
  21.         self.date = [aDecoder decodeObjectForKey:DATE];  
  22.           
  23.     }  
  24.       
  25.     return self;  
  26. }  
  27.   
  28. @end  


在使用的时候

[objc]  view plain  copy
  1. -(void)storeChatLogWithFile  
  2. {  
  3. //    获取路径  
  4.     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
  5.     NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"chatlog.plist"];  
  6.     NSFileManager *fileM = [NSFileManager defaultManager];  
  7. //    判断文件是否存在,不存在则直接创建,存在则直接取出文件中的内容  
  8.     if (![fileM fileExistsAtPath:filePath]) {  
  9.         [fileM createFileAtPath:filePath contents:nil attributes:nil];  
  10.     }  
  11.     NSMutableArray *chatLogArray = [NSMutableArray arrayWithContentsOfFile:filePath];  
  12.     if ((chatLogArray.count == 0)) {  
  13.         chatLogArray = [NSMutableArray arrayWithCapacity:1];  
  14.     }  
  15.       
  16. //    要保存的自定义模型  
  17.     ChatLogModel *chatmodel = [[ChatLogModel alloc] init];  
  18.     chatmodel.name = @"张三";  
  19.     chatmodel.isVideo = YES;  
  20. //    获取当前时间  
  21.     NSDate *currentDate = [NSDate date];  
  22.     NSDateFormatter *formatter = [[NSDateFormatter alloc] init];  
  23.     [formatter setDateFormat:@"MM-dd hh:mm:ss"];  
  24.     NSString *dateString = [formatter stringFromDate:currentDate];  
  25.     chatmodel.date = dateString;  
  26.    
  27.     [chatLogArray addObject:chatmodel];  
  28.   
  29. /* 
  30.     这是正常的保存和取出数组内容到文件 
  31.     存 
  32.     [chatLogArray writeToFile:filePath atomically:YES]; 
  33.     取 
  34.     NSMutableArray *array = [NSMutableArray arrayWithContentsOfFile:filePath]; 
  35. */  
  36.    
  37.       
  38. //    注意 数组中保存的是自定义模型,要想把数组保存在文件中,应该用下面的方法  
  39. //    存  
  40.     [NSKeyedArchiver archiveRootObject:chatLogArray toFile:filePath];  
  41. //    取  
  42.     NSArray *array = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];  
  43.     NSLog(@"array:%@",array);  
  44.   
  45. }  

猜你喜欢

转载自blog.csdn.net/wakice/article/details/78188150
今日推荐