objective-C中的序列化(serialize)與還原序列化(deserialize)

來源:互聯網
上載者:User
obj-c中有一類對象:NSArray,NSDictionary,NSString,NSNumber,NSDate,NSData以及它們的可變版本(指NSMutableArray,NSMutableDictionary...這一類) ,都可以方便的將自身的資料以某種格式(比如xml格式)序列化後儲存成本地檔案。範例程式碼:NSArrayTest.h
#import <Foundation/Foundation.h>#define FILE_NAME @"/tmp/data.txt"@interface NSArrayTest : NSObject {}-(void) Test;@end
NSArrayTest.m
#import "NSArrayTest.h"@implementation NSArrayTest-(void) Test{NSArray *arr = [NSArray arrayWithObjects:@"one",@"two",@"three",nil];//注:最後一個要以nil結尾[arr writeToFile:FILE_NAME atomically:YES];//(序列化為xml格式後)儲存檔案NSArray *arr2 = [NSArray arrayWithContentsOfFile:FILE_NAME];//read fileNSLog(@"%@",arr2);}@end
運行結果:2011-03-03 14:20:01.501 pList[1246:a0f] (
    one,
    two,
    three
)如果查看/tmp/data.txt,能看到下面的內容:
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"><plist version="1.0"><array><string>one</string><string>two</string><string>three</string></array></plist>

即NSArray預設是以xml格式來序列化對象的.如果你用來存放資料的類是自己定義的,並不是上面這些預置的對象,那麼就要藉助正式協議NSCoding來實現序列化和還原序列化。

比如,我們有一個自己的類Sample.h

#import <Foundation/Foundation.h>@interface Sample : NSObject<NSCoding> {NSString* name;int magicNumber;float shoeSize;NSMutableArray *subThingies;}@property(copy) NSString* name;@property int magicNumber;@property float shoeSize;@property (retain) NSMutableArray *subThingies;-(id) initWithName:(NSString *)n magicNumber:(int)m shoeSize:(float) ss;@end
這裡我們定義幾個不同類型的屬性,有字串,有整數,有浮點數,還有一個可變長的數組對象Sample.m
#import "Sample.h"@implementation Sample@synthesize name;@synthesize magicNumber;@synthesize shoeSize;@synthesize subThingies;-(id) initWithName:(NSString *)n magicNumber:(int)m shoeSize:(float)ss{if (self=[super init]){self.name = n;self.magicNumber = m;self.shoeSize = ss;self.subThingies = [NSMutableArray array];}return (self);}-(void) dealloc{[name release];[subThingies release];[super dealloc];}//將對象編碼(即:序列化)-(void) encodeWithCoder:(NSCoder *)aCoder{[aCoder encodeObject:name forKey:@"name"];[aCoder encodeInt:magicNumber forKey:@"magicNumber"];[aCoderencodeFloat:shoeSize forKey:@"shoeSize"];[aCoderencodeObject:subThingies forKey:@"subThingies"];}//將對象解碼(還原序列化)-(id) initWithCoder:(NSCoder *)aDecoder{if (self=[super init]){self.name = [aDecoder decodeObjectForKey:@"name"];self.magicNumber = [aDecoder decodeIntForKey:@"magicNumber"];self.shoeSize = [aDecoder decodeFloatForKey:@"shoeSize"];self.subThingies = [aDecoder decodeObjectForKey:@"subThingies"];}return (self);}-(NSString*) description{NSString *description = [NSString stringWithFormat:@"%@:%d/%.1f %@",name,magicNumber,shoeSize,subThingies];return (description);}@end
注意其中的:encodeWithCoder與initWithCoder,這是NSCoding協議中定義的二個方法,用來實現對象的編碼與解碼。其實現也不複雜,利用的是key-value的經典雜湊結構。當然一般在編碼中,對於key的名字字串,建議用define以常量方式事先定義好,以避免開發人員字串鍵入錯誤。測試一下:
#import <Foundation/Foundation.h>#import "Sample.h"int main (int argc, const char * argv[]) {    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];Sample *s1 = [[Sample alloc] initWithName:@"thing1" magicNumber:42 shoeSize:10.5];[s1.subThingies addObject:@"1"];[s1.subThingies addObject:@"2"];NSLog(@"%@",s1);NSData*data1 = [NSKeyedArchiver archivedDataWithRootObject:s1];//將s1序列化後,儲存到NSData中[s1 release];[data1 writeToFile:@"/tmp/data.txt" atomically:YES];//持久化儲存成物理檔案NSData *data2 = [NSData dataWithContentsOfFile:@"/tmp/data.txt"];//讀取檔案Sample *s2 = [NSKeyedUnarchiver unarchiveObjectWithData:data2];//還原序列化    NSLog(@"%@",s2); [pool drain];    return 0;}

運行結果:

2011-03-03 14:36:48.540 pList[1322:a0f] thing1:42/10.5 (
    1,
    2
)
2011-03-03 14:36:48.548 pList[1322:a0f] thing1:42/10.5 (
    1,
    2
)

查看/tmp/data.txt,能看到以下內容:

由於經過了編碼,裡面的內容沒有象前面的NSArray那樣可讀性強。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.