iOS专题iOSiOS数据持久化

iOS 数据存储方式

2015-08-18  本文已影响671人  gcoder_io

Plist(NSArray/NSDictionary):

  • 支持的数据类型有NSString、 NSNumber、NSDate、 NSArray、NSDictionary、BOOL、NSInteger、NSFloat等系统定义的数据类型,底层是基于key-value的NSDictionary。
读取项目中预置plist文件:
- (void) testPlist{
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"plist"];
    NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
    NSLog(@"data===%@", data);
}
运行期创建的plist文件读取数据:
- (void) testReadPlist{
    // 存储路径
    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *plistPath = [paths objectAtIndex:0];
    NSString *filename=[plistPath stringByAppendingPathComponent:@"test_new.plist"];
    // 查询
    NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile:filename];
    NSLog(@"%@", data);
}
运行期创建新plist并写入数据:
- (void) testWriteNewPlist{
    // 存储路径
    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *plistPath = [paths objectAtIndex:0];
    NSString *filename=[plistPath stringByAppendingPathComponent:@"test_new.plist"];
    
    // 原始数据
    NSMutableDictionary *data = [NSMutableDictionary dictionary];
    // key存在value被覆盖,key不存在新增
    [data setObject:@(1) forKey:@"age"];

    // 写入数据
    BOOL success = [data writeToFile:filename atomically:YES];
    NSLog(@"success===%@", success?@"YES":@"NO");
    
    // 查询验证结果
    NSMutableDictionary *data1 = [[NSMutableDictionary alloc] initWithContentsOfFile:filename];
    NSLog(@"%@", data1);
}
运行期创建的plist修改、删除数据:
- (void) testModifyAndRemovePlist{
    // 存储路径
    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *plistPath = [paths objectAtIndex:0];
    NSString *filename=[plistPath stringByAppendingPathComponent:@"test_new.plist"];
    
    NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile:filename];
    NSLog(@"%@", data);
    
    // 修改数据
    [data setObject:@(2) forKey:@"age"];
    // 删除数据
    [data removeObjectForKey:@"name"];
    
    // 写入数据
    BOOL success = [data writeToFile:filename atomically:YES];
    NSLog(@"success===%@", success?@"YES":@"NO");
    
    // 查询验证结果
    NSMutableDictionary *data1 = [[NSMutableDictionary alloc] initWithContentsOfFile:filename];
    NSLog(@"%@", data1);
}

NSUserDefault:

支持的数据类型有NSString、 NSNumber、NSDate、 NSArray、NSDictionary、BOOL、NSInteger、NSFloat等系统定义的数据类型,如果要存放自定义的对象(如自定义的类对象),则必须将其转换成NSData存储。

- (void) testUserDefaults{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    // 写入
    [userDefaults setInteger:2 forKey:@"age"];
    [userDefaults setObject:@"test_name" forKey:@"name"];
    // 强制写入磁盘
    [userDefaults synchronize];

    // 读取
    NSInteger age = [userDefaults integerForKey:@"age"];
    NSString *name = [userDefaults objectForKey:@"name"];
    NSLog(@"%ld===%@", age, name);
    
    //修改
    [userDefaults setInteger:3 forKey:@"age"];
    
    // 删除
    [userDefaults removeObjectForKey:@"name"];
    // 强制写入磁盘
    [userDefaults synchronize];
    
    NSLog(@"%ld===%@", [userDefaults integerForKey:@"age"],
          [userDefaults objectForKey:@"name"]);
}

NSCoding:

可存储自定义对象,局限:一次性做读取和存储操作,不可局部增量操作

#import <Foundation/Foundation.h>
#import "ZZBaseEntity.h"

@interface TestUser : ZZBaseEntity<NSCoding>

@property (nonatomic, copy) NSString *name;
@property (nonatomic, assign) NSInteger age;
@property (nonatomic, assign) NSInteger pid;

@end
#import "TestUser.h"

@implementation TestUser

- (void) setValue:(id)value forUndefinedKey:(NSString *)key{
    if ([key isEqualToString:@"id"]) {
         self.pid = [value integerValue];
    }
}

- (void)encodeWithCoder:(NSCoder *)aCoder{
    if (self.name){
        [aCoder encodeObject:self.name forKey:@"name"];
    }
    if (self.age){
        [aCoder encodeInteger:self.age forKey:@"age"];
    }
    if (self.pid){
        [aCoder encodeInteger:self.pid forKey:@"pid"];
    }
}

- (id)initWithCoder:(NSCoder *)aDecoder{
    if (self == [super init]) {
        self.name = [aDecoder decodeObjectForKey:@"name"];
        self.age = [aDecoder decodeIntegerForKey:@"age"];
        self.pid = [aDecoder decodeIntegerForKey:@"pid"];
    }
    return self;
}

- (NSString *)description{
    return [NSString stringWithFormat:@"%ld===%ld===%@", self.pid, self.age, self.name];
}
@end
- (void) testUserDefaults{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    // 写入
    TestUser *user = [[TestUser alloc]init];
    user.name = @"test_name";
    user.age = 2;
    
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:user];
    [userDefaults setObject:data forKey:@"user"];
    [userDefaults synchronize];
    
    // 读取
    NSData *data1 = [userDefaults objectForKey:@"user"];
    TestUser *user1 = [NSKeyedUnarchiver unarchiveObjectWithData:data1];
    NSLog(@"user1===%@", user1);
}

Coredata:

Coredata是对sqlite数据库ORM实现。

Sqlite:

基本操作同Android中Sqlite。

本文作者:gcoder.io
本文链接:http://gcoder-io.github.io/2015/06/09/ios-base-io/
版权声明: 本博客所有文章均为原创,转载请注明作者及出处

上一篇 下一篇

猜你喜欢

热点阅读