数据存储之属性列表Plist

常用的数据存储有属性列表、偏好设置、归档、sqlite、coreData。上一博客了解了沙盒,现在了解下属性列表Plist。

数据存储之属性列表Plist

通常通过NSArray、NSDictionary集合类的WriteToFile:atomically方法将他们存储到属性列表中。在属性列表能保存的数据类型如下

所以可以序列化的类有以下这些:

NSArray、NSMutableArray、NSDictionary、NSMutableDictionary、NSData、NSMutableData、NSDate、NSString、NSMutableString、NSNumber

对Boolean类型的数据进行读写时,需先转为NSNumber类型,然后通过NSNumber的boolValue方法读取。

//

//  ViewController.m

//  Plist

//

//  Created by City--Online on 15/4/21.

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

//



#import "ViewController.h"

#import "Student.h"



@interface ViewController ()



@end



@implementation ViewController



- (void)viewDidLoad {

    [super viewDidLoad];

    NSArray *array= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);

    NSString *path=[array objectAtIndex:0];



#if 0 //数组

     NSString *filePath=[path stringByAppendingPathComponent:@"students.plist"];

     NSLog(@"%@",filePath);

#if 0//数组写数据

    NSArray *array1=[[NSArray alloc]initWithObjects:@"a",[NSDate date],@20.9,[NSNumber numberWithBool:YES],nil]

    //YES 通过atomically参数让该方法将数据写入辅助文件,而不是写入指定位置。成功写入该文件后,该辅助文件将被复制到第一个参数指定的位置.这是更安全的写入方法,因为如果应用程序在保存期间崩溃,则现有文件不会被破坏。虽增加开销,但在大多数情况还是值得的。

    [array1 writeToFile:filePath atomically:YES];

#elif 1  //数组读数据

//    NSArray *array1=[[NSArray alloc]initWithContentsOfFile:filePath];

    NSArray *array1=[NSArray arrayWithContentsOfFile:filePath];

    for (NSString *s in array1) {

        NSLog(@"%@",s);

    }

#endif

    

#elif 1 //字典

     NSString *filePath=[path stringByAppendingPathComponent:@"studentsdic.plist"];

     NSLog(@"%@",filePath);

#if 0//字典写入

    NSDictionary *dic=[[NSDictionary alloc]initWithObjects:@[@"a",@"b",@"c"] forKeys:@[@"1",@"2",@"3"]];

    [dic writeToFile:filePath atomically:NO];

#elif 1

    //字典读数据

//    NSDictionary *dic=[NSDictionary dictionaryWithContentsOfFile:filePath];

    NSDictionary *dic=[[NSDictionary alloc]initWithContentsOfFile:filePath];

    for (NSString * s in dic.allKeys) {

        NSLog(@"%@",[dic objectForKey:s]);

    }

#endif

#endif



    

}



- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}



@end

//    //获取沙盒根目录

//    NSString *home=NSHomeDirectory();

//     NSLog(@"沙盒根目录:%@\n\n",home);

//

//    //获取Documents目录 不建议采用

//    NSString *documents=[home stringByAppendingPathComponent:@"Documents"];

//    NSLog(@"字符串拼接获取Documents:%@\n\n",documents);

//

//    //NSUserDomainMask 代表从用户文件夹下找

//    //YES  代表展开路径中的波浪字符“~” NO ~/Documents

//    NSArray *array=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, NO);

//    // 在iOS中,只有一个目录跟传入的参数匹配,所以这个集合里面只有一个元素

//    NSString *documents1=[array objectAtIndex:0];

//    NSLog(@"通过方法NSSearchPathForDirectoriesInDomains获取Documents:%@\n\n",documents1);

//

//    //获取tmp文件目录

//    NSLog(@"tmp 文件目录:%@\n\n",NSTemporaryDirectory());

//

//    //获取Library/Caches:

//    NSArray *arrayCaches=NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);

//    NSLog(@"Library/Caches:%@",arrayCaches[0]);

//

//    //Library/Preference:通过NSUserDefaults类存取该目录下的设置信息

 数据存储之属性列表Plist

数据存储之属性列表Plist

你可能感兴趣的:(plist)