将Array、Dictionary等集合类的序列化和反序列化
by Elton on 2010年02月15日, under iPhone
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]);
-
}
-
输出如下:

相关文章:



七月 19th, 2010 on 17:13
字典是反序的。。。
八月 15th, 2010 on 18:56
A genuinely great article by you my pal. We have bookmarked this web page and can appear again following several days to verify for just about any new posts that you just make.
八月 16th, 2010 on 03:40
Awesome article which has obtained me considering concerning the prospective of the idea. Definitely actually awesome.