objective-c中的本地化 *** 作(序列化,归档)

objective-c中的本地化 *** 作(序列化,归档),第1张

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

@H_502_6@ Human.h:

@H_502_6@

[plain]  view plain copy #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: @H_502_6@

@H_502_6@

copy #import "Human.h"   @implementation Human   @synthesize age;   @synthesize name;   @synthesize child;   //-(Nsstring *)description   //{   //    Nsstring *des = [Nsstring stringWithFormat:@"%d,%@,%@",age,name,child];   //    return des;   //}   @end  
上面的重写描述被注释掉了,我们先看未修改前的输出: @H_502_6@

@H_502_6@ main.m:

@H_502_6@

copy #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;   }  
@H_502_6@

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


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

@H_502_6@

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


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

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

copy                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;   }  

@H_502_6@


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

Human.h:

copy @interface Human : NSObject<NSCoding>   @end  
Human.m: @H_502_6@

copy -(Nsstring *)description       Nsstring *des = [Nsstring stringWithFormat:@"%d,108); List-style:decimal-leading-zero outsIDe; color:inherit; line-height:18px">     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: @H_502_6@

copy #import <Foundation/NSKeyedArchiver.h>   int main(int argc, const char * argv[])       @autoreleasepool {                  human1.age=20;           //定义好自定义对象后使用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);   }  
@H_502_6@

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

20,(null)

20,(null)

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

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

copy         [NSKeyedArchiver archiveRootObject:human1 tofile:@"/Users/holydancer/Desktop/tmp.txt"];//直接写入磁盘           Human *human3=[NSKeyedUnarchiver unarchiveObjectWithfile:@"/Users/holydancer/Desktop/tmp.txt"];//从磁盘直接读取为ID类型           NSLog(@"\n%@\n%@",human3);   }  

@H_502_6@

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

20,(null)

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

总结

以上是内存溢出为你收集整理的objective-c中的本地化 *** 作(序列化,归档)全部内容,希望文章能够帮你解决objective-c中的本地化 *** 作(序列化,归档)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1055765.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存