데이터 영구 화 - plist 파일 기록

2229 단어 plist
데이터 영구 화, 흔히 볼 수 있 는 4 가지: 압축 파일, plist 파일, sqlite, coreData. 오늘 복습 하 는 것 은 plist 파일 읽 기와 쓰기 입 니 다.
//
//  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

좋은 웹페이지 즐겨찾기