IOS 开发学习二十 数据持久化:读写属性列表示例

//
//  PropertiesUtils.m
//  smarthome
//
//  Created by 谢厂节 on 15/5/25.
//  Copyright (c) 2015年 WHR. All rights reserved.
//

#import "PropertiesUtils.h"

@implementation PropertiesUtils


-(void)save:(int)index :(NSString *)value{
    NSString* fileName = [self filePath:@"Properties.plist"];

    if ([[NSFileManager defaultManager]fileExistsAtPath:fileName]) {
        NSMutableArray* data = [[NSMutableArray alloc]initWithContentsOfFile:fileName];
        [data replaceObjectAtIndex:index withObject:value];
        
        [data writeToFile:fileName atomically:YES];
    }
}
-(NSString*)load:(int)index{
    NSString* fileName = [self filePath:@"Properties.plist"];
    if ([[NSFileManager defaultManager]fileExistsAtPath:fileName]) {
        NSArray* data = [[NSArray alloc]initWithContentsOfFile:fileName];
        return [data objectAtIndex:index];
    }
    else
        return nil;
}
-(NSArray*)loadAll{
    NSString* fileName = [self filePath:@"Properties.plist"];
    if ([[NSFileManager defaultManager]fileExistsAtPath:fileName]) {
        NSArray* data = [[NSArray alloc]initWithContentsOfFile:fileName];
        return data;
    }
    else
        return nil;
}
-(NSString *)filePath:(NSString *)fileName{
    NSArray* myPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString* myDocPath = [myPaths objectAtIndex:0];
    NSString* filePath = [myDocPath stringByAppendingPathComponent:fileName];
    return filePath;
}

@end

你可能感兴趣的:(ios,数据,持久化)