Plist文件

//
//  main.m
//  OC3_Plist文件 用 writeToFile创建
//
//  Created by zhangxueming on 15/6/23.
//  Copyright (c) 2015年 zhangxueming. All rights reserved.
//

#import <Foundation/Foundation.h>
//plist文件
//plist文件的根节点只能为数组(NSArray)或者字典(NSDictionary)
//存储的对象: NSArray NSDictionary NSDate NSData Boolean NSNumber NSString

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSArray *array = [[NSArray alloc] initWithObjects:@"one",@"two",[NSNumber numberWithInt:123],[NSDate date], nil];
        //在指定目录下没有对应的plist文件, 会自动创建该文件
        BOOL ret = [array writeToFile:@"/Users/zhangxueming/Desktop/Test/array.plist" atomically:YES];
        if (ret) {
            NSLog(@"文件写入成功");
        }
        else
        {
            NSLog(@"文件写入失败");
        }
        
        NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"1",@"one",@"2",@"two",[NSNumber numberWithInt:345],@"num",[@"ios" dataUsingEncoding:NSUTF8StringEncoding],@"ios",[NSDate date],@"date",array,@"array", nil];
        ret = [dict writeToFile:@"/Users/zhangxueming/Desktop/Test/dict.plist" atomically:YES];
        if (ret) {
            NSLog(@"文件写入成功");
        }
        else{
            NSLog(@"文件写入失败");
        }
        
    }
    return 0;
}



----------
//

//  main.m

//  OC4_Plist文件读取

//

//  Created by zhangxueming on 15/6/23.

//  Copyright (c) 2015年 zhangxueming. All rights reserved.

//

 

#import <Foundation/Foundation.h>

 

int main(int argc, const char * argv[]) {

    @autoreleasepool {

        NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:@"/Users/zhangxueming/Desktop/ios1509/Day21_文件归档/OC4_Plist文件读取/apple.plist"];

        if(dict)

        {

            NSLog(@"dict = %@", dict);

        }

        

        

        NSArray *array = [NSArray arrayWithContentsOfFile:@"/Users/zhangxueming/Desktop/Test/array.plist"];

        if (array) {

            NSLog(@"array = %@", array);

        }

    }

    return 0;

}

 

 

 

posted @ 2015-06-23 12:32  sirzhang  阅读(270)  评论(0编辑  收藏  举报
AmazingCounters.com