holydancer原创,如需转载,请在显要位置注明:
转自holydancer的CSDN专栏,原文地址:http://blog.csdn.net/holydancer/article/details/7371643
先介绍一个自定义类描述的方法description,一般情况下,一个自定义类我们在用%@输出的时候,给出的是一个内存地址,我们在该类的.m文件里重写description方法,来修改输出内容,呆会儿我们要用到这个方法来验证今天学习内容,所以先看一段代码熟悉一下:
Human.h:
- #import <Foundation/Foundation.h>
- @interface Human : NSObject
- {
- int age;
- NSString *name;
- Human *child;
- }
- @property int age;
- @property (copy)NSString *name;
- @property (retain)Human *child;
- @end
Human.m:
- #import "Human.h"
- @implementation Human
- @synthesize age;
- @synthesize name;
- @synthesize child;
- //-(NSString *)description
- //{
- // NSString *des = [NSString stringWithFormat:@"%d,%@,%@",age,name,child];
- // return des;
- //}
- @end
上面的重写描述被注释掉了,我们先看未修改前的输出:
main.m:
- #import <Foundation/Foundation.h>
- #import "Human.h"
- int main(int argc, const char * argv[])
- {
- @autoreleasepool {
- Human *human1=[[Human alloc]init];
- Human *human2=[[Human alloc]init];
- human1.child=human2;
- human1.name=@"holydancer";
- human1.age=22;
- NSLog(@"%@",human1);
- }
- return 0;
- }
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.看代码:
- #import <Foundation/Foundation.h>
- #import "Human.h"
- int main(int argc, const char * argv[])
- {
- @autoreleasepool {
- NSData *data=[[NSData alloc]init];
- NSString *string=[[NSString alloc]init];
- NSArray *array=[[NSArray alloc]init];
- NSDictionary *dictionary=[[NSDictionary alloc]init];
- [data writeToFile:@"/Users/holydancer/Desktop/text1.txt" atomically:YES];
- [string writeToFile:@"/Users/holydancer/Desktop/text2.txt" atomically:YES];
- [array writeToFile:@"/Users/holydancer/Desktop/text3.txt" atomically:YES];
- [dictionary writeToFile:@"/Users/holydancer/Desktop/text4.txt" atomically:YES];
- //atomically参数是指是否将写入文件的内容开启保护机制,如果开启,会在复制时创建临时文件进行复制,以免写入失败破坏原始文件。安全,但是会消耗内存。
- //上面的文件地址,如果不存在的话会自动生成。有的话会覆盖原有文件内容。
- }
- return 0;
- }
以上四种是COCOA自带可以写入磁盘文件的类型,但是我们常常用到自定义类,可是里面并没有writeToFile方法,怎么办呢?这时NSData的作用就体现出来了,我们可以把任意自定义类转化成NSData格式即可,这个过程我们称之为编码,或者archive归档,需要将自定义类实现NSCoding协议并重写encodeWithCoder和initWithCoder两个方法,分别用以编码和反编码。然后在编码时会用NSCoder的子类NSKeyedArchiver和NSKeyedUnarchiver分别调用archivedDataWithRootObject和unarchiveObjectWithData来启动自定义类中重写的那两个方法,类似于回调。看代码:
Human.h:
- #import <Foundation/Foundation.h>
- @interface Human : NSObject<NSCoding>
- {
- int age;
- NSString *name;
- Human *child;
- }
- @property int age;
- @property (copy)NSString *name;
- @property (retain)Human *child;
- @end
Human.m:
- #import "Human.h"
- @implementation Human
- @synthesize age;
- @synthesize name;
- @synthesize child;
- -(NSString *)description
- {
- NSString *des = [NSString stringWithFormat:@"%d,%@,%@",age,name,child];
- return des;
- }
- -(void)encodeWithCoder:(NSCoder *)aCoder//要一一对应
- {
- [aCoder encodeInt:age forKey:@"age"];
- [aCoder encodeObject:name forKey:@"name"];
- [aCoder encodeObject:child forKey:@"child"];
- }
- -(id)initWithCoder:(NSCoder *)aDecoder//和上面对应
- {
- if (self=[super init]) {
- self.age=[aDecoder decodeIntForKey:@"age"];
- self.name=[aDecoder decodeObjectForKey:@"name"];
- self.child=[aDecoder decodeObjectForKey:@"child"];
- }
- return self;
- }
- @end
main.m:
- #import <Foundation/Foundation.h>
- #import "Human.h"
- #import <Foundation/NSKeyedArchiver.h>
- int main(int argc, const char * argv[])
- {
- @autoreleasepool {
- Human *human1=[[Human alloc]init];
- Human *human2=[[Human alloc]init];
- human1.age=20;
- human1.name=@"holydancer";
- human1.child=human2;
- //定义好自定义对象后使用NSCoding的子类调用archivedDataWithRootObject方法进行archive
- NSData *data1=[NSKeyedArchiver archivedDataWithRootObject:human1];
- //转成NSData类型后就可以写入本地磁盘了
- [data1 writeToFile:@"/Users/holydancer/Desktop/tmp.txt" atomically:YES];
- //倒过来的话先读取磁盘文件
- NSData *data2=[NSData dataWithContentsOfFile:@"/Users/holydancer/Desktop/tmp.txt"];
- Human *human3=[NSKeyedUnarchiver unarchiveObjectWithData:data2];
- NSLog(@"%@,%@",human1,human3);
- }
- return 0;
- }
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方法下在还有一个方法,不错,这个方法可以直接将自定义类写入本地磁盘,所以上在的代码我们还可以这样写:
- #import <Foundation/Foundation.h>
- #import "Human.h"
- #import <Foundation/NSKeyedArchiver.h>
- int main(int argc, const char * argv[])
- {
- @autoreleasepool {
- Human *human1=[[Human alloc]init];
- Human *human2=[[Human alloc]init];
- human1.age=20;
- human1.name=@"holydancer";
- human1.child=human2;
- [NSKeyedArchiver archiveRootObject:human1 toFile:@"/Users/holydancer/Desktop/tmp.txt"];//直接写入磁盘
- Human *human3=[NSKeyedUnarchiver unarchiveObjectWithFile:@"/Users/holydancer/Desktop/tmp.txt"];//从磁盘直接读取为id类型
- NSLog(@"\n%@\n%@",human1,human3);
- }
- return 0;
- }
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
相关推荐
这儿的运行时系统扮演的角色类似于Objective-C语言的操作系统,Objective-C基于该系统来工作。本文档将具体介绍NSObject类以及Objective-C程序是如何和运行时系统交互的。特别地,本文档还给出来怎样在运行时动态地加
Runtime系统是一种用于Objective-C语言的操作系统,它使OC语言工作起来。 Runtime的核心是在运行时动态操作类和消息分发给其他对象,本源代码详细介绍了怎么使用它们。 博客:http://blog.csdn.net/y550918116j
《objective-c程序设计》通过大量的实例系统地介绍了objective-c语言的基本概念、语法规则、框架、类库及开发环境。读者在阅读《objective-c程序设计》后,可以掌握objective-c语言的基本内容,并进行实际的iphone/...
希望这个简单的Objective-C语言教程能够为你提供一个入门的起点。Objective-C是一种面向对象的编程语言,广泛应用于Mac OS和iOS开发。通过学习Objective-C的基本语法、类和...祝你在学习Objective-C的过程中取得成功!
《Objective-C 程序设计(第4版)》已经为iOS 5和Xcode4.2中的重大变更做了全面更新,最大的改动是引入了自动引用计数(ARC),并详细说明了如何在Objective-C编程过程中使用ARC提升和简化内存管理。
用Objective-C语言实现了各种设计模式,收集各种例子,方便大家学习和普及设计模式。.zip用Objective-C语言实现了各种设计模式,收集各种例子,方便大家学习和普及设计模式。.zip用Objective-C语言实现了各种设计...
中文名: Objective-C基础教程 作者: Mark Dalrymple Scott Knaster译者: 高朝勤 杨越 刘霞图书 分类: 软件 资源格式: PDF 版本: 扫描版 出版社: 人民邮电出版社 书号: ISBN: 9787115208774 发行时间: 2009...
C语言主要知识点巩固(学习Objective-C 的前提),PPT格式。
本书全面而系统地讲述Objective-C语言的基础知识和面向对象编程的重要概念,结合实例介绍了Cocoa工具包的优秀特性和其中的框架,以及继承、复合、源文件组织、内存管理、对象初始化和类别创建等众多重要的面向对象...
Objective-C 基础教程,从入门到精通 Objective-C 基础教程,从入门到精通,是你学习 Objective-C 的经典之作。
在windows系统下搭建Objective-C开发环境,由于公司要做iphone开发,个人学习objc(Objective-C)语法,所以在windows7下搭建了一个objc开发环境,现把自己整理的帮助文档拿来与大家分享,希望能对大家有所帮助。...
Objective-C 是一种通用、面向对象的编程语言,广泛应用于 macOS 和 iOS 等苹果平台的开发中。它是 C 语言的超集,结合了 Smalltalk 式的消息传递机制和 C++ 的特性。Objective-C 是苹果公司采用的主要编程语言之一...
elcome to Learn Objective- C on the Mac! This book is designed to teach you the basics of the Objective- C language. Objective- C is a superset of C and is the language used by many (if not most) ...
iOS开发中实体类的序列化为Json格式与Json格式反序列化为实体类对象,需要在支持ARC的项目中使用,非ARC项目需要手懂添加资源释放
这是一个基于Objective-C语言的基础案例集 旨在用于给初学者快速了解Objective-C语言的语法 .zip
《Objective-C2.0程序设计(原书第2版)》是Objective-C领域的经典著作,对Objective-C进行了系统而全面的阐述,权威性毋庸置疑。全书共分为四大部分:第一部分全面讲解了Objective-C语言的基础知识,包括类、对象、...
Objective-C是扩展C的面向对象编程语言,也是iPhone开发用到的主要语言。本书结合理论知识与示例程序,全面而系统地讲述Objective-C编程...附录中还介绍了如何从其他语言过渡到Objective-C。 本书适合各类开发人员阅读
Objective-C高级编程 iOS与OS X多线程和内存管理.pdf