iOS学习笔记-032.数据的读取——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,不能存储自定义对象
支持的数据类型:

Array
Dictionary
Boolean
Date
Number
String

三、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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值