来自:http://blog.prosight.me/index.php/tag/writetofile
Objective-C的集合类序列化到文件中或者从文件中反序列化其实很简单,请看下面的示例 代码:
-
-
@"Hefeweizen", @"IPA", @"Pilsner", @"Stout", nil];
-
-
array, @"array", @"Stout", @"dark", @"Hefeweizen", @"wheat", @"IPA",
-
@"hoppy", nil];
-
-
// 得到documents directory的路径
-
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
-
NSUserDomainMask, YES);
-
if ([paths count] > 0)
-
{
-
// Array的保存路径
-
stringByAppendingPathComponent:@"array.out"];
-
-
// dictionary的保存路径
-
stringByAppendingPathComponent:@"dict.out"];
-
-
// 保存array
-
[array writeToFile:arrayPath atomically:YES];
-
-
// 保存dictionary
-
[dictionary writeToFile:dictPath atomically:YES];
-
-
// 从文件中读取回来
-
-
NSLog(@"Beer: %@", element);
-
-
NSLog(@"%@ Style: %@", key, [dictionary valueForKey:key]);
-
}
-
输出如下: