iOS 数据持久化(一)

iOS中有五种持久化数据的方式:属性列表、对象归档、NSUserDefault、SQLite3和Core Data

本文章讲述通过属性列表的方式持久化数据,这个方法也是我们平时最经常用到的方式。比如应用程序的配置和个性化的设置,一般都是通过属性列表(properties list) plist文件来存储和读取的。


FOUNDATION_EXPORT NSArray *NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory directory, NSSearchPathDomainMask domainMask, BOOL expandTilde);

这个函数的作用:创建一个目录搜索路径的列表。

创建一个路径字符串列表指定域中指定的目录。列表中的顺序,你应该搜索目录。

PS:

通过此方法返回的目录可能不存在。这种方法只是给你请求目录的适当位置。根据应用的需求,它可能是开发者创建合适的目录或者在任何两者之间。

下面是一个把数据以plist的形式保存到本地的自定义类

//
//  Plist.h
//  PlistDemo
//
//  Created by swplzj on 13-11-20.
//  Copyright (c) 2013年 swplzj. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Plist : NSObject

- (void)modifyData:(NSString *)dataKey Value:(NSString *)dataValue;
- (void)deleteDataWithDataKey:(NSString *)key;

@end

//
//  Plist.m
//  PlistDemo
//
//  Created by swplzj on 13-11-20.
//  Copyright (c) 2013年 swplzj. All rights reserved.
//

#import "Plist.h"

@implementation Plist

- (id)init
{
    if (self = [super init]) {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        //获取完整路径
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSLog(@"homeDirectory = %@", documentsDirectory);
        NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"student.plist"];
        //判断是否已经创建文件
        if ([[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
            NSLog(@"student.plist文件已经存在!");
        }else {
            //plist文件没有被创建
            NSMutableDictionary *rootDic = [[NSMutableDictionary alloc] init];
            NSMutableDictionary *subDic = [[NSMutableDictionary alloc] init];
            [rootDic setObject:subDic forKey:@"student"];
            [subDic release];
            //写入到文件
            [rootDic writeToFile:plistPath atomically:YES];
            [rootDic release];
        }
    }
    
    return self;
}

//把数据写入到plist文件中
- (void)modifyData:(NSString *)dataKey Value:(NSString *)dataValue
{
    //获取路径
    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"student.plist"];
    NSMutableDictionary *rootDic = [[[NSMutableDictionary alloc] initWithContentsOfFile:path] mutableCopy];
    NSMutableDictionary *studentInfo = [rootDic objectForKey:@"student"];
    NSString *name = [studentInfo objectForKey:dataKey];
    name = dataValue;
    [studentInfo setValue:name forKey:dataKey];
    //写入到文件
    [rootDic writeToFile:path atomically:YES];
    NSLog(@"Key - Value: %@ - %@",dataKey, dataValue );
    [rootDic release];
}

//根据key来删除某一行的数据
- (void)deleteDataWithDataKey:(NSString *)key
{
    //获取路径
    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"student.plist"];
    NSMutableDictionary *rootDic = [[[NSMutableDictionary alloc] initWithContentsOfFile:path] mutableCopy];
    NSMutableDictionary *studentInfo = [rootDic objectForKey:@"student"];
    [studentInfo removeObjectForKey:key];
    NSLog(@"Delete data key - value: %@ - %@", key, [studentInfo objectForKey:key]);
    [rootDic setValue:studentInfo forKey:@"student"];
    //写入到文件
    [rootDic writeToFile:path atomically:YES];
    [rootDic release];
}

@end

在RootViewController里面的代码

//
//  RootViewController.m
//  PlistDemo
//
//  Created by swplzj on 13-11-20.
//  Copyright (c) 2013年 swplzj. All rights reserved.
//

#import "RootViewController.h"
#import "Plist.h"

@interface RootViewController ()

@property (retain, nonatomic) Plist *plistFile;

@property (retain, nonatomic) IBOutlet UITextField *keyTF;
@property (retain, nonatomic) IBOutlet UITextField *valueTF;
@property (retain, nonatomic) IBOutlet UITextField *deleteKeyTF;
@end

@implementation RootViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    _plistFile = [[Plist alloc] init];
}

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

- (IBAction)addOrModifyBtnClicked:(id)sender {
    if (![_keyTF.text isEqualToString:@""] && ![_valueTF.text isEqualToString:@""]) {
        [_plistFile modifyData:_keyTF.text Value:_valueTF.text]; 
    }
}

- (IBAction)deleteBtnClicked:(id)sender {
    if (![_deleteKeyTF.text isEqualToString:@""]) {
        [_plistFile deleteDataWithDataKey:_deleteKeyTF.text];
    }
}

- (void)dealloc {
    [_plistFile release];
    [_keyTF release];
    [_valueTF release];
    [_deleteKeyTF release];
    [super dealloc];
}
@end

效果


前往文件夹:

/Users/issuser/Library/Application Support/iPhone Simulator/6.1/Applications/A40F6B2C-002A-4797-B501-3ABCBE59723A/Documents





plist文件中的内容:




好了,这就是通过属性列表plist文件来达到数据持久化的目的。点击这里可以下载plistDemo代码


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值