生活随笔
收集整理的這篇文章主要介紹了
将Array、Dictionary等集合类的序列化和反序列化
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Objective-C的集合類序列化到文件中或者從文件中反序列化其實很簡單,請看下面的示例代碼:
NSArray?*array =?[NSArray?arrayWithObjects: @"Hefeweizen", @"IPA", @"Pilsner", @"Stout",?nil]; NSDictionary?*dictionary =?[NSDictionary?dictionaryWithObjectsAndKeys: array, @"array", @"Stout", @"dark", @"Hefeweizen", @"wheat", @"IPA", @"hoppy",?nil]; // 得到documents directory的路徑 NSArray?*paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); if?([paths count]?>?0) { // Array的保存路徑 NSString??*arrayPath =?[[paths objectAtIndex:0] stringByAppendingPathComponent:@"array.out"]; // dictionary的保存路徑 NSString??*dictPath =?[[paths objectAtIndex:0] stringByAppendingPathComponent:@"dict.out"]; // 保存array [array writeToFile:arrayPath atomically:YES]; // 保存dictionary [dictionary writeToFile:dictPath atomically:YES]; // 從文件中讀取回來 NSArray?*arrayFromFile =?[NSArray?arrayWithContentsOfFile:arrayPath]; NSDictionary?*dictFromFile =?[NSDictionary?dictionaryWithContentsOfFile:dictPath]; for?(NSString?*element in arrayFromFile) NSLog(@"Beer: %@", element); for?(NSString?*key in dictFromFile) NSLog(@"%@ Style: %@", key,?[dictionary valueForKey:key]); } 輸出如下:
http://blog.prosight.me/index.php/2010/02/582
轉載于:https://www.cnblogs.com/1208/archive/2012/07/05/2578210.html
總結
以上是生活随笔為你收集整理的将Array、Dictionary等集合类的序列化和反序列化的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。