数据持久化-Plist文件写入_移动开发_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > 移动开发 > 数据持久化-Plist文件写入

数据持久化-Plist文件写入

 2014/10/30 15:09:48  xclidongbo  程序员俱乐部  我要评论(0)
  • 摘要:数据持久化,常见4种:归档,plist文件,sqlite,coreData.今天复习的是plist文件读写.////ViewController.m//Test_Plist////Createdbylidongboon14/10/30.//Copyright(c)2014年lidongbo.Allrightsreserved.//#import"ViewController.h"@interfaceViewController(
  • 标签:list 文件 数据

数据持久化,常见4种:归档,plist文件,sqlite,coreData.今天复习的是plist文件读写.

class="brush:objc;gutter:true;">//
//  ViewController.m
//  Test_Plist
//
//  Created by lidongbo on 14/10/30.
//  Copyright (c) 2014年 lidongbo. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    /*
     读取plist文件的内容.
     */
    NSString * path = [[NSBundle mainBundle] pathForResource:@"Person" ofType:@"plist"];
    NSMutableDictionary * data = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
    NSLog(@"%@",data);
    
    /*
     获取Document文件夹中plist文件的路径
     */
    NSMutableArray * mArr = [[NSMutableArray alloc] initWithObjects:@"英语",@"数据",@"法语",@"日语",@"德语", nil];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString * plistPath = [paths objectAtIndex:0];
    NSLog(@"%@",plistPath);
    
//    NSString * fileName = [plistPath stringByAppendingString:@"/Person.plist"];
    NSString * fileName = [plistPath stringByAppendingPathComponent:@"Person.plist"];
    
    /*
        赋值
     */
    [data setObject:mArr forKey:@"kemu"];
    [data setObject:@"14" forKey:@"age"];
    /*
     plist文件写入
     */
    [data writeToFile:fileName atomically:YES];

    NSMutableDictionary * data1 = [[NSMutableDictionary alloc] initWithContentsOfFile:fileName];
    NSLog(@"%@",data1);
    
    /*
     plist文件可以多次写入.
     */
    NSMutableArray * mmArr = [[NSMutableArray alloc] initWithObjects:@"1",@"2",@"3", nil];
    [data setObject:mmArr forKey:@"kemu"];
    
    [data writeToFile:fileName atomically:YES];
    
    NSMutableDictionary * data2 = [[NSMutableDictionary alloc] initWithContentsOfFile:fileName];
    NSLog(@"______%@",data2);

    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
上一篇: 手机产品设计之用户引导 下一篇: 没有下一篇了!
发表评论
用户名: 匿名