数据存储之属性列表Plist

简介:

常用的数据存储有属性列表、偏好设置、归档、sqlite、coreData。上一博客了解了沙盒,现在了解下属性列表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类存取该目录下的设置信息

 

相关文章
|
7月前
|
存储
结构体存储学生信息
结构体存储学生信息
|
6月前
|
XML JSON 数据格式
在 plist 中轻松转换两种格式
在 plist 中轻松转换两种格式
|
3月前
|
存储 NoSQL 数据库
请解释一下键值存储数据库的工作原理,并提供一个使用键值存储数据库的实际应用场景。
请解释一下键值存储数据库的工作原理,并提供一个使用键值存储数据库的实际应用场景。
60 0
|
4月前
|
存储 缓存 NoSQL
键值存储
键值存储
102 1
FastReport数据头有行有AutoSize属性,则数据头和数据区会有空白。
FastReport数据头有行有AutoSize属性,则数据头和数据区会有空白。
|
7月前
|
存储
结构体存储数据
结构体存储数据
|
11月前
|
存储 XML JSON
plist 文件格式转换器
plist 文件是一种用于存储应用程序配置信息的文件格式,其中包含应用程序的各种设置和数据。
|
C语言
【C 语言】文件操作 ( 配置文件读写 | 写出或更新配置文件 | 追加键值对数据 | 更新键值对数据 )
【C 语言】文件操作 ( 配置文件读写 | 写出或更新配置文件 | 追加键值对数据 | 更新键值对数据 )
110 0