03-PIist存储-偏好设置-自定义对象归档

 

//  ViewController.m
//  05-PIist存储
#import "ViewController.h"
#import "Person.h"

@interface ViewController ()

@end

@implementation ViewController
// 点击存储的时候调用
- (IBAction)save:(id)sender {
    Person *p = [[Person alloc] init];
    // Plist注意:不能存储自定义对象
    // Plist:数组和字典,
    // 如何判断一个对象能不能使用Plist,就看下有没有writeToFile
    NSArray *arr = @[@"123",@1,p];

    
    // 获取应用的文件夹(应用沙盒)
//    NSString *homePath = NSHomeDirectory();
    
    // 获取temp
//    NSTemporaryDirectory();
    
    // 获取Cache文件路径
    // NSSearchPathDirectory:搜索的目录
    // NSSearchPathDomainMask:搜索范围 NSUserDomainMask:表示在用户的手机上查找
    // expandTilde 是否展开全路径,如果没有展开,应用的沙盒路径就是~
    // 存储一定要要展开路径
    NSString *cachePaht = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
    
    
    // 拼接文件名
    NSString *filePath = [cachePaht stringByAppendingPathComponent:@"personArr.plist"];
    
    
    
    NSLog(@"%@",cachePaht);
    
    
    // File:文件的全路径
    [arr writeToFile:filePath atomically:YES];
    
}

- (IBAction)read:(id)sender {
    
    NSString *cachePaht = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
    
    
    // 拼接文件名
    NSString *filePath = [cachePaht stringByAppendingPathComponent:@"arr.plist"];
    
   NSArray *arr = [NSArray arrayWithContentsOfFile:filePath];
    
    NSLog(@"%@",arr);
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

 

//
//  Person.h
//  05-PIist存储

#import <Foundation/Foundation.h>

@interface Person : NSObject

@end
//
//  Person.m
//  05-PIist存储

#import "Person.h"

@implementation Person

@end

 

06-偏好设置
//
//  ViewController.m
//  06-偏好设置
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

// 偏好设置存储:
// 好处:1.不需要关心文件名
// 2.快速做键值对存储

// 底层:就是封装了一个字典
// account:xmg pwd:123 rmbPwd:YES
- (IBAction)save:(id)sender {
    
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    
    [userDefaults setObject:@"xmg" forKey:@"account"];
    [userDefaults setObject:@"123" forKey:@"pwd"];
    [userDefaults setBool:YES forKey:@"rmbPwd"];
    // 在iOS7之前,默认不会马上把跟硬盘同步
    // 同步
    [userDefaults synchronize];
    
    
    
}
- (IBAction)read:(id)sender {
   NSString *pwd = [[NSUserDefaults standardUserDefaults] objectForKey:@"pwd"];
    
    NSLog(@"%@",pwd);
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

 

自定义归档

//
//  ViewController.m
//  07-自定义对象归档

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

@interface ViewController ()

@end

@implementation ViewController
- (IBAction)save:(id)sender {
    
    Person *p = [[Person alloc] init];
    p.age = 18;
    
    // 获取cache
    NSString *cachePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
    
    // 获取文件的全路径
    NSString *filePath = [cachePath stringByAppendingPathComponent:@"person.data"];
    
    // 把自定义对象归档
    [NSKeyedArchiver archiveRootObject:p toFile:filePath];
    
}
- (IBAction)read:(id)sender {
    
    // 获取cache
    NSString *cachePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
    
    // 获取文件的全路径
    NSString *filePath = [cachePath stringByAppendingPathComponent:@"person.data"];
    
    // 解档
    Person *p = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
    
    NSLog(@"%d",p.age);
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
//
//  Person.h
//  07-自定义对象归档
#import <Foundation/Foundation.h>

// 如果一个自定义对象想要归档,必须遵守NSCoding协议,实现协议方法。
@interface Person : NSObject<NSCoding>
@property (nonatomic, assign) int age;
@property (nonatomic, strong) NSString* name;
@end
//
//  Person.m
//  07-自定义对象归档

#import "Person.h"

@implementation Person

// 什么时候调用:自定义对象归档的时候

// 作用:用来描述当前对象里面的哪些属性需要归档
- (void)encodeWithCoder:(NSCoder *)aCoder
{
    // name
    [aCoder encodeObject:_name forKey:@"name"];
    
    // age
    [aCoder encodeInt:_age forKey:@"age"];
    
}


// 什么时候调用:解档对象的时候调用

// 作用:用来描述当前对象里面的哪些属性需要解档
// initWithCoder:就是用来解析文件的。
- (id)initWithCoder:(NSCoder *)aDecoder
{
    // super:NSObject
#warning 什么时候需要调用initWithCoder
    if (self = [super init]) {
        
        // 注意:一定要给成员变量赋值
        // name
       _name = [aDecoder decodeObjectForKey:@"name"];
        
        // age
       _age = [aDecoder decodeIntForKey:@"age"];

    }
    return self;
    
    
}
@end

 

//
//  RedView.h
//  07-自定义对象归档
#import <UIKit/UIKit.h>

@interface RedView : UIView

@end
//  RedView.m
//  07-自定义对象归档
#import "RedView.h"

@implementation RedView

// 解析文件都会调用这个方法

- (id)initWithCoder:(NSCoder *)aDecoder
{
    
    // 只要父类遵守了NSCoding,就调用initWithCoder
    // 先初始化父类
    if (self = [super initWithCoder:aDecoder]) {
        NSLog(@"%s",__func__);
    }
    
    return self;
}


// 通过代码初始化的时候,调用init方法,底层就会调用initWithFrame
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        NSLog(@"%s",__func__);
    }
    return self;
}
@end

 

posted @ 2017-03-31 14:42  laugh  阅读(207)  评论(0编辑  收藏  举报