iOS: plist实例

 1 //
 2 //  main.m
 3 //  OSXDemo0601_plist
 4 //
 5 //  Created by yao_yu on 14-6-3.
 6 //  Copyright (c) 2014年 yao_yu. All rights reserved.
 7 //
 8 
 9 #import <Foundation/Foundation.h>
10 
11 BOOL isfile(NSString *path)
12 {
13     NSFileManager *fileManager = [NSFileManager defaultManager];
14     BOOL isdir = NO;
15     if ([fileManager fileExistsAtPath:path isDirectory: &isdir]) {
16         return isdir == NO;
17     }
18     return NO;
19 }
20 
21 BOOL isdir(NSString *path)
22 {
23     NSFileManager *fileManager = [NSFileManager defaultManager];
24     BOOL isdir = NO;
25     if ([fileManager fileExistsAtPath:path isDirectory: &isdir]) {
26         return isdir == YES;
27     }
28     return NO;
29 }
30 
31 int main(int argc, const char * argv[])
32 {
33 
34     @autoreleasepool {
35         //得到文档目录
36         NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
37         NSString *documentDir = [paths objectAtIndex:0];
38         
39         //得到(创建)自定义数据目录
40         NSString *testDir = [[documentDir stringByAppendingPathComponent:@"YY"] stringByAppendingPathComponent:@"Data"];
41         NSFileManager *fileMan = [NSFileManager defaultManager];
42         if (!isdir(testDir))
43             [fileMan createDirectoryAtPath:testDir withIntermediateDirectories:YES attributes:nil error:nil];
44         
45         //创建plist文件, 序列化字典
46         NSString * plistfile = [testDir stringByAppendingPathComponent:@"my.plist"];
47         if (!isfile(plistfile))
48         {
49             NSMutableDictionary *a = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@1,@"A", @2, @"B", nil];
50             [a writeToFile:plistfile atomically:YES];
51         }
52         
53         //读取plist文件, 反序列化
54         NSMutableDictionary *plist = [NSMutableDictionary dictionaryWithContentsOfFile:plistfile];
55         [plist setObject:@"中华人民共和国" forKey:@"国家"];
56         NSLog(@"%@", plist);
57     }
58     return 0;
59 }

 

posted @ 2014-06-03 16:36  一花一世界,一叶一乾坤  阅读(327)  评论(0编辑  收藏  举报