iOS文檔序列化(對象歸檔)

來源:互聯網
上載者:User

標籤:

對象歸檔:

概念:

對象歸檔是指將對象寫入檔案儲存在硬碟,當再次又一次開啟程式時,能夠還原這些對象。也稱:對象序列化、對象持久化。


資料持久性的方式(事實上就是3類)

1,NSKeyedArchiver——對象歸檔

2,NSUserDefaults  3,屬性列表 

4,SQLite資料庫  5,Core Data資料庫


歸檔方式:

對Foundation庫中對象進行歸檔

自己定義對象進行歸檔(須要實現歸檔協議,NSCoding)


歸檔與屬性列表的差別:

1,歸檔後的檔案是加密的,屬性列表是明文的。

2,屬性列表不能儲存自己定義物件類型,歸檔能夠。



代碼實現:

//對象歸檔,對象反歸檔int main(int argc, char * argv[]){    @autoreleasepool {//        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));        //歸檔(序列化)        NSArray *array = @[@"abc",@"123",@1234];                NSString *homePath = NSHomeDirectory();        NSLog(@"homePath :%@",homePath);        NSString *path = [homePath stringByAppendingPathComponent:@"test.archive"];  //檔案和副檔名隨便取                BOOL success = [NSKeyedArchiver archiveRootObject:array toFile:path];        if (success) {            NSLog(@"archive success");        }    }    return 0;}


這是就產生了一個加密檔案。

開啟

6270 6c69 7374 3030 d401 0203 0405 081c1d54 2474 6f70 5824 6f62 6a65 6374 73582476 6572 7369 6f6e 5924 6172 6368 69766572 d106 0754 726f 6f74 8001 a609 0a121314 1555 246e 756c 6cd2 0b0c 0d0e 5624636c 6173 735a 4e53 2e6f 626a 6563 74738005 a30f 1011 8002 8003 8004 5361 62635331 3233 1104 d2d2 1617 181b 5824 636c6173 7365 735a 2463 6c61 7373 6e61 6d65a219 1a57 4e53 4172 7261 7958 4e53 4f626a65 6374 574e 5341 7272 6179 1200 0186a05f 100f 4e53 4b65 7965 6441 7263 68697665 7208 1116 1f28 3235 3a3c 4349 4e556062 6668 6a6c 7074 777c 8590 939b a4acb100 0000 0000 0001 0100 0000 0000 00001e00 0000 0000 0000 0000 0000 0000 0000


 

int main(int argc, char * argv[]){    @autoreleasepool {//        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));        //歸檔(序列化)//        NSArray *array = @[@"abc",@"123",@1234];//        //        NSString *homePath = NSHomeDirectory();//        NSLog(@"homePath :%@",homePath);//        NSString *path = [homePath stringByAppendingPathComponent:@"test.archive"];  //檔案和副檔名隨便取//        //        BOOL success = [NSKeyedArchiver archiveRootObject:array toFile:path];//        if (success) {//            NSLog(@"archive success");//        }                //解歸檔(還原序列化)        NSString *homePath = NSHomeDirectory();        NSString *path = [homePath stringByAppendingPathComponent:@"test.archive"];        NSArray *array = [NSKeyedUnarchiver unarchiveObjectWithFile:path];        NSLog(@"NSKeyedUnarchiver:%@",array);    }    return 0;}


這種方法的缺陷:

一個對象歸檔成一個檔案,當對象許多時很麻煩。

以下的方式能夠解決問題:

歸檔:

1,使用NSData執行個體作為歸檔的儲存資料

2,加入歸檔的內容(設定key和value)

3,完畢歸檔

4,將歸檔資料存入磁碟中


解歸檔:

1,從磁碟中讀取檔案,產生NSData執行個體

2,依據Data執行個體建立和初始化解歸檔執行個體

3,解歸檔,依據key訪問value的值。



iOS文檔序列化(對象歸檔)

聯繫我們

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