iOS對象序列化

來源:互聯網
上載者:User

標籤:des   style   c   class   blog   code   

系統對象的歸檔我就不介紹了,這個不複雜,自己看一下就會了。

我在這裡主要介紹自訂對象的歸檔。

 

Sample.h檔案

 9 #import <Foundation/Foundation.h>10 11 @interface Sample : NSObject<NSCoding> {12     13     NSString* name;14     int magicNumber;15     float shoeSize;16     NSMutableArray *subThingies;17 }18 19 @property(copy) NSString* name;20 @property int magicNumber;21 @property float shoeSize;22 @property (retain) NSMutableArray *subThingies;23 24 25 -(id) initWithName:(NSString *)n magicNumber:(int)m shoeSize:(float) ss;26 27 @end

Sample.m檔案

 9 #import "Sample.h"10 11 @implementation Sample12 13 @synthesize name;14 @synthesize magicNumber;15 @synthesize shoeSize;16 @synthesize subThingies;17 18 -(id) initWithName:(NSString *)n magicNumber:(int)m shoeSize:(float)ss19 {20     if (self=[super init])21     {22         self.name = n;23         self.magicNumber = m;24         self.shoeSize = ss;25         self.subThingies = [NSMutableArray array];26     }27     return (self);28 }29 30 -(void) dealloc31 {32     [name release];33     [subThingies release];34     [super dealloc];35 }36 37 //將對象編碼(即:序列化)38 -(void) encodeWithCoder:(NSCoder *)aCoder39 {40     [aCoder encodeObject:name forKey:@"name"];41     [aCoder encodeInt:magicNumber forKey:@"magicNumber"];42     [aCoder    encodeFloat:shoeSize forKey:@"shoeSize"];43     [aCoder    encodeObject:subThingies forKey:@"subThingies"];44 }45 46 //將對象解碼(還原序列化)47 -(id) initWithCoder:(NSCoder *)aDecoder48 {49     if (self=[super init])50     {51         self.name = [aDecoder decodeObjectForKey:@"name"];52         self.magicNumber = [aDecoder decodeIntForKey:@"magicNumber"];53         self.shoeSize = [aDecoder decodeFloatForKey:@"shoeSize"];54         self.subThingies = [aDecoder decodeObjectForKey:@"subThingies"];55     }56     return (self);57     58 }59 60 61 -(NSString*) description62 {63     NSString *description = [NSString stringWithFormat:@"%@:%d/%.1f %@",name,magicNumber,shoeSize,subThingies];64     return (description);65 }66 67 @end

使用模板

NSString *path = [NSString stringWithFormat:@"%@/Documents/archive.dat", NSHomeDirectory()];Sample *s1 = [[Sample alloc] initWithName:@"thing1" magicNumber:42 shoeSize:10.5];    [s1.subThingies addObject:@"1"];  [s1.subThingies addObject:@"2"];  //序列化  NSData  *data1 = [NSKeyedArchiver archivedDataWithRootObject:s1];//將s1序列化後,儲存到NSData中  [s1 release];  [data1 writeToFile:path atomically:YES];//持久化儲存成物理檔案  //還原序列化  NSData *data2 = [NSData dataWithContentsOfFile:path];//讀取檔案   Sample *s2 = [NSKeyedUnarchiver unarchiveObjectWithData:data2];//還原序列化     NSLog(@"%@",s2);  

如果是多個這類對象組成的數組,序列化也很簡單,只須對這個數組進行序列化

 1 Sample *s1 = [[Sample alloc] initWithName:@"thing1" magicNumber:42 shoeSize:10.5];   2 [s1.subThingies addObject:@"1"];   3 [s1.subThingies addObject:@"2"];   4       5 Sample *s2 = [[Sample alloc] initWithName:@"thing2" magicNumber:22 shoeSize:22.2];   6 [s2.subThingies addObject:@"22"];   7 [s2.subThingies addObject:@"22"];   8       9 NSArray *array = [NSArray arrayWithObjects:s1, s2, nil]; 10 [s1 release];11 [s2 release];12 13 NSString *path = [NSString stringWithFormat:@"%@/Documents/archive.dat", NSHomeDirectory()];14 //序列化  15 NSData  *data1 = [NSKeyedArchiver archivedDataWithRootObject:array];16 [data1 writeToFile:path atomically:YES];//持久化儲存成物理檔案  17 //房序列化  18 NSData *data2 = [NSData dataWithContentsOfFile:path];//讀取檔案  19 NSArray *array2 = [NSKeyedUnarchiver unarchiveObjectWithData:data2];//還原序列化  20 NSLog(@"%@",array2); 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.