iOS学习笔记-032.数据的读取——plist写入
来源:互联网 发布:windows命令 编辑:程序博客网 时间:2024/05/22 14:48
- 数据的读取plist写入
- 一iOS应用数据存取的常用方式
- 二XML属性列表PList
- PList写入
- PList的局限性
- 三PList写入示例
- 四PList写入图示
数据的读取——plist写入
一、iOS应用数据存取的常用方式
- XML属性列表 —— PList
- NSKeyedArchiver 归档
- Preference 偏好设置
- SQLite3
- Core Data
二、XML属性列表——PList
1.PList写入
属性列表是一种XML格式的文件,扩展名为plist
如果对象是NSArray、NSDictionary类型,可以使用writeToFile:atomically:方法直接写入到属性列表文件
如果对象是NSString、NSData类型,也可以使用writeToFile:atomically:方法写入对应的文件
说明:atomically(写入原子性)
YES:先创建一个临时文件,直到内容完成后再导入目标文件
NO:直接写入文件
注意:如果所指定保存文件的路径不存在,写入文件方法不会报错,文件也不会被保存!
2.PList的局限性
只有支持的数据类型可以被序列化,存储到plist中。无法将其他Cocoa对象存储到plist,不能存储自定义对象
支持的数据类型:
ArrayDictionaryBooleanDateNumberString
三、PList写入示例
//// ViewController.m// 03_UIView26_PList写入//// Created by 杞文明 on 2016/01/08 01:02:49 星期五// Copyright © 2016年 杞文明. All rights reserved.//#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; [self createButtons];}#pragma mark - 创建按钮-(void)createButtons{ //1.实例化写入数组的按钮 UIButton * arrayBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; //2.设置尺寸 [arrayBtn setFrame:CGRectMake(150, 200, 80, 40)]; //3.设置文字 [arrayBtn setTitle:@"写入数组" forState:UIControlStateNormal]; //4.设置监听 [arrayBtn addTarget:self action:@selector(wirteArray) forControlEvents:UIControlEventTouchUpInside]; //5.添加到view中 [self.view addSubview:arrayBtn]; //======写入字典的按钮===== UIButton * dicBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [dicBtn setFrame:CGRectMake(150, 400, 80, 40)]; [dicBtn setTitle:@"写入字典" forState:UIControlStateNormal]; [dicBtn addTarget:self action:@selector(wirteDict) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:dicBtn];}#pragma mark - 写入数组-(void)wirteArray{ //1.创建一个数据 NSMutableArray * arrayListM = [[NSMutableArray alloc]init]; for (NSInteger i=0; i<20; i++) { [arrayListM addObject:[NSString stringWithFormat:@"我是明爷%03ld号",i]]; } //2.获取写入的位置 NSArray * document = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString * path = [document[0] stringByAppendingPathComponent:@"xmarray.plist"]; //3.写入 [arrayListM writeToFile:path atomically:YES]; NSLog(@"%@",path);}#pragma mark - 写入字典-(void)wirteDict{ //1.创建字典 NSMutableDictionary * dictM = [[NSMutableDictionary alloc]init]; for (NSInteger i=0; i<30; i++) { NSInteger arrayLength = arc4random_uniform(30)+6; NSMutableArray *array = [NSMutableArray array]; for (NSInteger j=0; j< arrayLength; j++) { [array addObject:[NSString stringWithFormat:@"你猜猜这是item几---%03ld---%03ld",i,j]]; } [dictM setValue:array forKey:[NSString stringWithFormat:@"这是集合--%03ld",i]]; } //2.获取写入位置 NSArray* document = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString * path = [document[0] stringByAppendingPathComponent:@"xmdict.plist"]; //3.写入 [dictM writeToFile:path atomically:YES]; NSLog(@"%@",path);}@end
四、PList写入图示
0 0
- iOS学习笔记-032.数据的读取——plist写入
- iOS学习笔记-035.数据的读取——系统偏好的写入和读取
- iOS中plist的创建,数据写入与读取
- iOS中plist,数据写入与读取
- IOS plist读取写入
- iOS学习笔记—— 数据持久化(Plist)
- iOS开发之数据的写入与读取(plist存储)
- IOS中Plist文件的读取与写入
- iOS 创建.写入.读取plist文件
- iOS 创建.写入.读取plist文件
- iOS 创建.写入.读取plist文件
- iOS plist文件写入和读取
- iOS开发读取plist数据
- ios学习笔记6--plist文件生成与读取
- Hadoop学习笔记——1.java读取Oracle中表的数据,创建新文件写入Hdfs
- iOS学习笔记:plist文件的读写
- plist文件的写入和读取
- ios 沙盒 plist 数据的读取和存储
- warning: dereferencing pointer ** does break strict-aliasing rules
- 文章标题
- android 8种对话框(Dialog)使用方法汇总
- bootstrap——组件(一、下拉菜单)
- 使用TortoiseGit向github上传代码
- iOS学习笔记-032.数据的读取——plist写入
- sql取出商品表每个类别的前3条记录
- Python爬虫从0开始的记录
- Centos6.8 搭建nexus
- C++三种继承方式
- Eclipse显示行号-定位某行
- JESD79-4 第4章 SDRAM命令描述与操作(4.5-4.7)
- drawText函数详解(转)
- 行者企业家天授唱诗人或是后互联网小鲜肉-2017年1月江西IDC排行榜与发展报告