IOS 개발 학습 20 데이터 지속 화: 읽 기 쓰기 속성 목록 예제

//
//  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

좋은 웹페이지 즐겨찾기