码迷,mamicode.com
首页 > 移动开发 > 详细

iOS: plist实例

时间:2014-06-06 20:49:19      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:c   style   class   blog   code   java   

bubuko.com,布布扣
 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 }
bubuko.com,布布扣

 

iOS: plist实例,布布扣,bubuko.com

iOS: plist实例

标签:c   style   class   blog   code   java   

原文地址:http://www.cnblogs.com/yaoyu126/p/3766032.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!