归档 object-c把数据写入到文件中

2024-08-21 09:08
文章标签 数据 object 归档 写入

本文主要是介绍归档 object-c把数据写入到文件中,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

holydancer原创

转自holydancer的CSDN专栏,原文地址:http://blog.csdn.net/holydancer/article/details/7371643


先介绍一个自定义类描述的方法description,一般情况下,一个自定义类我们在用%@输出的时候,给出的是一个内存地址,我们在该类的.m文件里重写description方法,来修改输出内容,呆会儿我们要用到这个方法来验证今天学习内容,所以先看一段代码熟悉一下:

Human.h:

[plain]  view plain copy
  1. #import <Foundation/Foundation.h>  
  2.   
  3. @interface Human : NSObject  
  4. {  
  5.     int age;  
  6.     NSString *name;  
  7.     Human *child;  
  8. }  
  9.   
  10. @property int age;  
  11. @property (copy)NSString *name;  
  12. @property (retain)Human *child;  
  13. @end  

Human.m:

[plain]  view plain copy
  1. #import "Human.h"  
  2.   
  3. @implementation Human  
  4. @synthesize age;  
  5. @synthesize name;  
  6. @synthesize child;  
  7.   
  8. //-(NSString *)description  
  9. //{  
  10. //    NSString *des = [NSString stringWithFormat:@"%d,%@,%@",age,name,child];  
  11. //    return des;  
  12. //}  
  13.   
  14. @end  

上面的重写描述被注释掉了,我们先看未修改前的输出:

main.m:

[plain]  view plain copy
  1. #import <Foundation/Foundation.h>  
  2. #import "Human.h"  
  3.   
  4. int main(int argc, const char * argv[])  
  5. {  
  6.   
  7.     @autoreleasepool {  
  8.         Human *human1=[[Human alloc]init];  
  9.         Human *human2=[[Human alloc]init];  
  10.         human1.child=human2;  
  11.         human1.name=@"holydancer";  
  12.         human1.age=22;  
  13.         NSLog(@"%@",human1);  
  14.               
  15.     }  
  16.     return 0;  
  17. }  

2012-03-20 08:47:32.980 category[304:403] <Human: 0x7ff2cb414380>


如果把human.m中的注释去掉的话输出结果如下:

2012-03-20 08:48:09.869 category[315:403] 22,holydancer,0,(null),(null)


很简单吧,这样就可以查看自己定义类的内容了,好了,下面就让我们来研究一下在objective-c中如何实现序列化。

在OC中,有四类对象是可以直接使用writeToFile方法将内容写入磁盘的,分别是NSString,NSArray,NSDictionary,NSData.看代码:

[plain]  view plain copy
  1. #import <Foundation/Foundation.h>  
  2. #import "Human.h"  
  3.   
  4. int main(int argc, const char * argv[])  
  5. {  
  6.   
  7.     @autoreleasepool {  
  8.       
  9.         NSData *data=[[NSData alloc]init];  
  10.         NSString *string=[[NSString alloc]init];  
  11.         NSArray *array=[[NSArray alloc]init];  
  12.         NSDictionary *dictionary=[[NSDictionary alloc]init];  
  13.                   
  14.         [data writeToFile:@"/Users/holydancer/Desktop/text1.txt" atomically:YES];  
  15.         [string writeToFile:@"/Users/holydancer/Desktop/text2.txt" atomically:YES];  
  16.         [array writeToFile:@"/Users/holydancer/Desktop/text3.txt" atomically:YES];  
  17.         [dictionary writeToFile:@"/Users/holydancer/Desktop/text4.txt" atomically:YES];  
  18.         //atomically参数是指是否将写入文件的内容开启保护机制,如果开启,会在复制时创建临时文件进行复制,以免写入失败破坏原始文件。安全,但是会消耗内存。  
  19.         //上面的文件地址,如果不存在的话会自动生成。有的话会覆盖原有文件内容。      
  20.     }  
  21.     return 0;  
  22. }  



以上四种是COCOA自带可以写入磁盘文件的类型,但是我们常常用到自定义类,可是里面并没有writeToFile方法,怎么办呢?这时NSData的作用就体现出来了,我们可以把任意自定义类转化成NSData格式即可,这个过程我们称之为编码,或者archive归档,需要将自定义类实现NSCoding协议并重写encodeWithCoder和initWithCoder两个方法,分别用以编码和反编码。然后在编码时会用NSCoder的子类NSKeyedArchiver和NSKeyedUnarchiver分别调用archivedDataWithRootObject和unarchiveObjectWithData来启动自定义类中重写的那两个方法,类似于回调。看代码:

Human.h:

[plain]  view plain copy
  1. #import <Foundation/Foundation.h>  
  2.   
  3. @interface Human : NSObject<NSCoding>  
  4. {  
  5.     int age;  
  6.     NSString *name;  
  7.     Human *child;  
  8. }  
  9.   
  10. @property int age;  
  11. @property (copy)NSString *name;  
  12. @property (retain)Human *child;  
  13. @end  

Human.m:

[plain]  view plain copy
  1. #import "Human.h"  
  2.   
  3. @implementation Human  
  4. @synthesize age;  
  5. @synthesize name;  
  6. @synthesize child;  
  7.   
  8. -(NSString *)description  
  9. {  
  10.     NSString *des = [NSString stringWithFormat:@"%d,%@,%@",age,name,child];  
  11.     return des;  
  12. }  
  13. -(void)encodeWithCoder:(NSCoder *)aCoder//要一一对应  
  14. {  
  15.     [aCoder encodeInt:age forKey:@"age"];  
  16.     [aCoder encodeObject:name forKey:@"name"];  
  17.     [aCoder encodeObject:child forKey:@"child"];  
  18. }  
  19. -(id)initWithCoder:(NSCoder *)aDecoder//和上面对应  
  20. {  
  21.     if (self=[super init]) {  
  22.         self.age=[aDecoder decodeIntForKey:@"age"];  
  23.         self.name=[aDecoder decodeObjectForKey:@"name"];  
  24.         self.child=[aDecoder decodeObjectForKey:@"child"];  
  25.     }  
  26.     return self;  
  27. }  
  28. @end  

main.m:

[plain]  view plain copy
  1. #import <Foundation/Foundation.h>  
  2. #import "Human.h"  
  3. #import <Foundation/NSKeyedArchiver.h>  
  4.   
  5. int main(int argc, const char * argv[])  
  6. {  
  7.   
  8.     @autoreleasepool {  
  9.       
  10.         Human *human1=[[Human alloc]init];  
  11.         Human *human2=[[Human alloc]init];  
  12.         human1.age=20;  
  13.         human1.name=@"holydancer";  
  14.         human1.child=human2;  
  15.         //定义好自定义对象后使用NSCoding的子类调用archivedDataWithRootObject方法进行archive  
  16.         NSData *data1=[NSKeyedArchiver archivedDataWithRootObject:human1];  
  17.         //转成NSData类型后就可以写入本地磁盘了  
  18.         [data1 writeToFile:@"/Users/holydancer/Desktop/tmp.txt" atomically:YES];  
  19.         //倒过来的话先读取磁盘文件  
  20.         NSData *data2=[NSData dataWithContentsOfFile:@"/Users/holydancer/Desktop/tmp.txt"];  
  21.         Human *human3=[NSKeyedUnarchiver unarchiveObjectWithData:data2];  
  22.         NSLog(@"%@,%@",human1,human3);  
  23.     }  
  24.     return 0;  
  25. }  

2012-03-20 10:10:29.871 category[458:403] 

20,holydancer,0,(null),(null)

20,holydancer,0,(null),(null)

有的同学一直不太清楚NSKeyedArchiver和NSKeyedUnarchiver是什么,调用的又是什么方法,大家可以在头文件里找到这样的信息:

可以发现,NSKeyedArchiver是NSCoder的子类,而archivedDataWithRootObject是里面的一个类方法,这时我们看到archivedDataWithRootObject方法下在还有一个方法,不错,这个方法可以直接将自定义类写入本地磁盘,所以上在的代码我们还可以这样写:

[plain]  view plain copy
  1. #import <Foundation/Foundation.h>  
  2. #import "Human.h"  
  3. #import <Foundation/NSKeyedArchiver.h>  
  4.   
  5. int main(int argc, const char * argv[])  
  6. {  
  7.   
  8.     @autoreleasepool {  
  9.       
  10.         Human *human1=[[Human alloc]init];  
  11.         Human *human2=[[Human alloc]init];  
  12.         human1.age=20;  
  13.         human1.name=@"holydancer";  
  14.         human1.child=human2;  
  15.         [NSKeyedArchiver archiveRootObject:human1 toFile:@"/Users/holydancer/Desktop/tmp.txt"];//直接写入磁盘  
  16.         Human *human3=[NSKeyedUnarchiver unarchiveObjectWithFile:@"/Users/holydancer/Desktop/tmp.txt"];//从磁盘直接读取为id类型  
  17.         NSLog(@"\n%@\n%@",human1,human3);  
  18.     }  
  19.     return 0;  
  20. }  


2012-03-20 10:16:43.561 category[475:403] 

20,holydancer,0,(null),(null)

20,holydancer,0,(null),(null)

最后,不得不说说cocoa中的方法命名,一个一个方法长得,虽然很人性化很好记,不过敲起来真是麻烦啊。


关键字:objective-c ,objective c , oc ,本地化,序列化,归档,archive ,NSCoder ,NSCoding , NSKeyedArchiver ,NSKeyedUnarchiver

这篇关于归档 object-c把数据写入到文件中的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1092729

相关文章

Java注解之超越Javadoc的元数据利器详解

《Java注解之超越Javadoc的元数据利器详解》本文将深入探讨Java注解的定义、类型、内置注解、自定义注解、保留策略、实际应用场景及最佳实践,无论是初学者还是资深开发者,都能通过本文了解如何利用... 目录什么是注解?注解的类型内置注编程解自定义注解注解的保留策略实际用例最佳实践总结在 Java 编程

一文教你Python如何快速精准抓取网页数据

《一文教你Python如何快速精准抓取网页数据》这篇文章主要为大家详细介绍了如何利用Python实现快速精准抓取网页数据,文中的示例代码简洁易懂,具有一定的借鉴价值,有需要的小伙伴可以了解下... 目录1. 准备工作2. 基础爬虫实现3. 高级功能扩展3.1 抓取文章详情3.2 保存数据到文件4. 完整示例

使用Java将各种数据写入Excel表格的操作示例

《使用Java将各种数据写入Excel表格的操作示例》在数据处理与管理领域,Excel凭借其强大的功能和广泛的应用,成为了数据存储与展示的重要工具,在Java开发过程中,常常需要将不同类型的数据,本文... 目录前言安装免费Java库1. 写入文本、或数值到 Excel单元格2. 写入数组到 Excel表格

python处理带有时区的日期和时间数据

《python处理带有时区的日期和时间数据》这篇文章主要为大家详细介绍了如何在Python中使用pytz库处理时区信息,包括获取当前UTC时间,转换为特定时区等,有需要的小伙伴可以参考一下... 目录时区基本信息python datetime使用timezonepandas处理时区数据知识延展时区基本信息

Qt实现网络数据解析的方法总结

《Qt实现网络数据解析的方法总结》在Qt中解析网络数据通常涉及接收原始字节流,并将其转换为有意义的应用层数据,这篇文章为大家介绍了详细步骤和示例,感兴趣的小伙伴可以了解下... 目录1. 网络数据接收2. 缓冲区管理(处理粘包/拆包)3. 常见数据格式解析3.1 jsON解析3.2 XML解析3.3 自定义

SpringMVC 通过ajax 前后端数据交互的实现方法

《SpringMVC通过ajax前后端数据交互的实现方法》:本文主要介绍SpringMVC通过ajax前后端数据交互的实现方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价... 在前端的开发过程中,经常在html页面通过AJAX进行前后端数据的交互,SpringMVC的controll

Pandas统计每行数据中的空值的方法示例

《Pandas统计每行数据中的空值的方法示例》处理缺失数据(NaN值)是一个非常常见的问题,本文主要介绍了Pandas统计每行数据中的空值的方法示例,具有一定的参考价值,感兴趣的可以了解一下... 目录什么是空值?为什么要统计空值?准备工作创建示例数据统计每行空值数量进一步分析www.chinasem.cn处

如何使用 Python 读取 Excel 数据

《如何使用Python读取Excel数据》:本文主要介绍使用Python读取Excel数据的详细教程,通过pandas和openpyxl,你可以轻松读取Excel文件,并进行各种数据处理操... 目录使用 python 读取 Excel 数据的详细教程1. 安装必要的依赖2. 读取 Excel 文件3. 读

Spring 请求之传递 JSON 数据的操作方法

《Spring请求之传递JSON数据的操作方法》JSON就是一种数据格式,有自己的格式和语法,使用文本表示一个对象或数组的信息,因此JSON本质是字符串,主要负责在不同的语言中数据传递和交换,这... 目录jsON 概念JSON 语法JSON 的语法JSON 的两种结构JSON 字符串和 Java 对象互转

C++如何通过Qt反射机制实现数据类序列化

《C++如何通过Qt反射机制实现数据类序列化》在C++工程中经常需要使用数据类,并对数据类进行存储、打印、调试等操作,所以本文就来聊聊C++如何通过Qt反射机制实现数据类序列化吧... 目录设计预期设计思路代码实现使用方法在 C++ 工程中经常需要使用数据类,并对数据类进行存储、打印、调试等操作。由于数据类