iOS 数据持久化之覆盖式存储(Plist ,Preference,归档)

一,Plist存储方式

     1,获得存储目录:  

NSString *pathForDocument = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;

     2,生成Plist文件并将想要存储的数组保存到Plist文件中:

NSString *fileName = [pathForDocument stringByAppendingPathComponent:@"123.plist"];
NSArray *arrayN = @[@"123",@"456",@"789"];
[arrayN writeToFile:fileName atomically:YES];

     3,读取之前保存的Plist文件:

NSArray *arrayResult = [NSArray arrayWithContentsOfFile:fileName];
NSLog(@"ReadPlist:%@",arrayResult);

二,Preference(偏好设置)存储方式:

      1,设置路径并保存需要存储的数据:

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:@"Austin.Kuture" forKey:@"Name"];
[userDefaults setBool:YES forKey:@"Sex"];
[userDefaults setInteger:18 forKey:@"Age"];  
[userDefaults synchronize];//立即存储

      2,读取偏好设置中的数据:

NSString *name = [userDefaults objectForKey:@"Name"];
BOOL sex = [userDefaults boolForKey:@"Sex"];
NSInteger age = [userDefaults integerForKey:@"Age"]; 
NSLog(@"Name:%@  Sex:%d  Age:%ld",name,sex,(long)age);

三,归档存储方式:

      1,遵守NSCoding协议,新建person类:

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface Person : NSObject<NSCoding>

@property (nonatomic,strong) UIImage *img;
@property (nonatomic,copy) NSString *name;
@property (nonatomic,assign) NSInteger age;

@end

     2,实现编码与解码方法,在person.m文件中:

#import "Person.h"

@implementation Person

- (instancetype)initWithCoder:(NSCoder *)aDecoder{
    
    if ([super init]){
        
        self.name = [aDecoder decodeObjectForKey:@"Name"];
        self.img = [aDecoder decodeObjectForKey:@"Image"];
        self.age = [aDecoder decodeIntegerForKey:@"Age"];
    }
    
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder{
    
    [aCoder encodeObject:self.img forKey:@"Image"];
    [aCoder encodeObject:self.name forKey:@"Name"];
    [aCoder encodeInteger:self.age forKey:@"Age" ];
}//如果需要归档的类是某个自定义类的子类时,就需要在归档和解档之前先实现父类的归档和解档方法。即 [super encodeWithCoder:aCoder] 和 [super initWithCoder:aDecoder] 方法

      3,对想要存储数据或对象的类中导入头文件:

#import "ViewController.h"
#import "Person.h"

@interface ViewController ()

@property (nonatomic,strong) UIImage *imgVC;
@property (nonatomic,copy) NSString *nameVC;
@property (nonatomic,assign) NSInteger ageVC;

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];
    //[self cacheForKeyAchiver];
    //[self cacheForKeyUnAchiver];
    
}

      4,对数据或对象进行归档:

- (void)cacheForKeyAchiver{
    
    UIImage *imgVC = [UIImage imageNamed:@"Kuture"];
    NSString *nameVC = [NSString stringWithFormat:@"Austin.Kuture"];
    NSInteger ageVC = 18;
    
    self.imgVC = imgVC;
    self.nameVC = nameVC;
    self.ageVC = ageVC;
    
    NSString *file = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"Person.data"];
    Person *person = [[Person alloc]init];
    person.img = self.imgVC;
    person.age = self.ageVC;
    person.name = self.nameVC;
    
    [NSKeyedArchiver archiveRootObject:person toFile:file];
    
}

      5,在需要该数据或对象的类中进行解档:

- (void)cacheForKeyUnAchiver{
    
    NSString *file = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"Person.data"];
    Person *person = [NSKeyedUnarchiver unarchiveObjectWithFile:file];
    if (person){
        
        NSLog(@"NewImage:%@  NewName:%@ NewAge:%ld",person.img,person.name,person.age);
    }
    
}

 

 

 

 

 

 

 

 

转载于:https://my.oschina.net/Kuture/blog/693033

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值