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
原创粉丝点击