iOS中的数据存储

  • 应用沙盒
  • 归档
  • 偏好设置
  • Plist

应用沙盒

Documents
Library -- Caches
        -- Preferences
tmp

归档

Person.h

#import 

@interface Person : NSObject 

/** 姓名 */
@property(nonatomic,copy) NSString* name;
/** 昵称 */
@property(nonatomic,copy) NSString* nickName;

@end

Person.m

#import "Person.h"

@implementation Person

// 归档:只要一个自定义对象归档的时候就会调用,告诉系统 哪些属性需要归档
- (void)encodeWithCoder:(NSCoder *)coder
{
    [coder encodeObject:_name forKey:@"name"]; // 给属性设置一个key
    [coder encodeObject:_nickName forKey:@"nickName"];
}


//解档:只要一个自定义对象解档的时候就会调用,告诉系统 解析key得到的值,赋值给哪个属性
- (id)initWithCoder:(NSCoder *)aDecoder{
//    什么时候调用 [super initWithCoder]
    if (self = [super init]) {
        _name = [aDecoder decodeObjectForKey:@"name"];
        _nickName = [aDecoder decodeObjectForKey:@"nickName"];
    }
    return self;
}

@end

ViewController.m

#import "ViewController.h"
#import "Person.h"
//---------------思路----------------
//1、创建自定义对象
//   重写encodeWithCoder 归档
//   initWithCoder方法  解档
//2、创建路径  /caches
//3、使用归档类方法进行存储
//   使用解档类方法进行解档
//-----------------------------------

@interface ViewController ()

- (IBAction)save:(id)sender;
- (IBAction)read:(id)sender;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (IBAction)save:(id)sender {
//    1、创建对象
    Person *p = [[Person alloc] init];
    p.name = @"李明";
    p.nickName = @"hahaha";
    
//    2.1、创建路径/tmp,使用函数
    NSString *cachesPath = NSTemporaryDirectory();
//    2.2、拼接路径;扩展名随意写,苹果对内容进行了加密
    NSString *path = [cachesPath stringByAppendingString:@"person.data"];
    
//    3、使用归档类方法进行存储
    [NSKeyedArchiver archiveRootObject:p toFile:path];
}

- (IBAction)read:(id)sender {
    
    NSString *cachesPath = NSTemporaryDirectory();
    NSString *path = [cachesPath stringByAppendingString:@"person.data"];
    
    Person *p = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
    NSLog(@"%@,%@",p.name,p.nickName);
}
@end

归档:什么时候调用super initWithCoder

#import "MyView.h"
@implementation MyView

// 解析文件的时候调用
// 作用:解析xib,storyboard调用
// 可以猜测 我们平时在storyboard中拖动的控件,是通过归档存储的,当创建的时候就解析 归档文件 然后实例化
- (id)initWithCoder:(NSCoder *)aDecoder{
    // 这里必须调用[super initWithCoder:aDecoder],super ->UIView
    // 什么时候调用[super initWithCoder:aDecoder],只要父类遵守了NSCoding协议,就调用[super initWithCoder:aDecoder]
    if(self = [super initWithCoder:aDecoder])
    {
        NSLog(@"调用了initWithCoder");
    }
    return self;
}

@end

偏好设置

#import "ViewController.h"

@interface ViewController ()

- (IBAction)save:(id)sender;
- (IBAction)read:(id)sender;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (IBAction)save:(id)sender {
//    /Library/Preferences
//    获取单例对象
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setValue:@"YChiong" forKey:@"name"];
    [userDefaults setObject:@"xmg" forKey:@"address"];
    [userDefaults setBool:YES forKey:@"isHandSome"];
    [userDefaults setInteger:25 forKey:@"age"];
}

- (IBAction)read:(id)sender {
    //    获取单例对象
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    
    NSString *name = [userDefaults valueForKey:@"name"];
    NSString *address = [userDefaults objectForKey:@"address"];
    BOOL isHandSome = [userDefaults boolForKey:@"isHandSome"];
    NSInteger age = [userDefaults integerForKey:@"age"];
    
    NSLog(@"%@,%@,%i,%i",name,address,isHandSome,age);
}
@end

Plist

#import "ViewController.h"

@interface ViewController ()
- (IBAction)save:(id)sender;
- (IBAction)read:(id)sender;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (IBAction)save:(id)sender {
//    1、创建数组或者字典
    NSArray *arr = @[@"YChiong",@25];
//    2、创建存储路径
//    2.1  /Library/Caches文件夹
//    参数:
//    NSSearchPathDirectory directory:获取哪个文件夹
//    NSSearchPathDomainMask domainMask:在哪个范围下获取 NSUserDomainMask:在用户的范围内搜索?
//    BOOL expandTilde:expandTilde是否展开全路径,YES:展开
//    返回值:一个数组
    NSString *cachePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
//    2.2 拼接路径
    NSString *plistPath = [cachePath stringByAppendingPathComponent:@"YChiong.plist"];
    
//    3、存储数组/字典 到 指定路径
    [arr writeToFile:plistPath atomically:YES];
}

- (IBAction)read:(id)sender {
//    怎么存,就怎么取
    NSString *cachePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
    NSString *plistPath = [cachePath stringByAppendingPathComponent:@"YChiong.plist"];
    
    NSArray *arr = [NSArray arrayWithContentsOfFile:plistPath];
    NSLog(@"%@",arr);
}
@end

你可能感兴趣的:(iOS中的数据存储)