歸檔




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

@interface Dog : NSObject<NSCoding>

/** 名稱 */
@property (nonatomic ,strong) NSString *name;

/** 體重 */
@property (nonatomic ,strong) NSString *weight;


@end

// Dog.m
#import "Dog.h"


@implementation Dog

// 歸檔哪些屬性
-(void)encodeWithCoder:(NSCoder *)aCoder
{
    [aCoder encodeObject:self.name forKey:@"name"];
    [aCoder encodeObject:self.weight forKey:@"weight"];
}

// 解檔哪些屬性
// 當解析一個文件的時候就會調用
-(instancetype)initWithCoder:(NSCoder *)aDecoder
{

    if (self = [super init]) {
        self.name = [aDecoder decodeObjectForKey:@"name"];
        self.weight = [aDecoder decodeObjectForKey:@"weight"];
    }
    return self;
}

@end

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

@class Dog;
@interface Person : NSObject<NSCoding>

/** 姓名 */
@property (nonatomic ,strong) NSString *name;
/** 年齡 */
@property (nonatomic, assign) NSInteger age;
/** 狗 */
@property (nonatomic ,strong) Dog *dog;

@end

// Person.m
#import "Person.h"

@implementation Person

//歸檔哪些屬性.
-(void)encodeWithCoder:(NSCoder *)aCoder {

    [aCoder encodeObject:self.name forKey:@"name"];
    [aCoder encodeInteger:self.age forKey:@"age"];
    [aCoder encodeObject:self.dog forKey:@"dog"];
}


//解檔哪些屬性
//當解析一個文件的時候就會調用.
-(instancetype)initWithCoder:(NSCoder *)aDecoder {

    if (self = [super init]) {
      self.name =   [aDecoder  decodeObjectForKey:@"name"];
      self.age =   [aDecoder decodeIntegerForKey:@"age"];
      self.dog = [aDecoder decodeObjectForKey:@"dog"];
    }
    return self;
}

@end

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

@interface ViewController : UIViewController

@end


// ViewController.m
#import "ViewController.h"
#import "Person.h"
#import "Dog.h"
@interface ViewController ()

@end

@implementation ViewController

- (IBAction)save:(id)sender {

    Person *per =  [[Person alloc] init];
    per.name = @"xmg";
    per.age = 10;

    Dog *dog = [[Dog alloc] init];
    dog.name = @"ww";
    per.dog = dog;

    //獲取目錄
    NSString *path = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
    //拼接路徑
    NSString *filePath = [path stringByAppendingPathComponent:@"person.data"];

    //歸檔
    //archiveRootObject底層會調用encodeWithCoder
    //就是要告訴它歸檔哪些屬性.
    [NSKeyedArchiver archiveRootObject:per toFile:filePath];
}
- (IBAction)read:(id)sender {

    //獲取目錄
    NSString *path = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
    //拼接路徑
    NSString *filePath = [path stringByAppendingPathComponent:@"person.data"];

    //解檔unarchiveObjectWithFile會調用initWithCoder
    //獲取當前這個對象的哪些屬性.
    Person *per =  [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
    NSLog(@"%@",per.name);
    NSLog(@"%ld",per.age);
    NSLog(@"%@",per.dog.name);
    NSLog(@"%@", per.dog.weight);
}

- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"%@", NSHomeDirectory());
}

@end

  • initWithCoder:淺析

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

@interface VCView : UIView

@end

// VCView.m
#import "VCView.h"

@interface VCView()


@property (weak, nonatomic) IBOutlet UIButton *btn;

@end


@implementation VCView

// 從nib文件當中加載完畢時調用 2
-(void)awakeFromNib {
    [super awakeFromNib];
    NSLog(@"%@",self.btn);
}

// 解析文件時會調用 1
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    
    if (self = [super initWithCoder:aDecoder]) { // UIView遵守了<NSCoding>協議,所以這里用了initWithCoder
        NSLog(@"===%@",self.btn);
    }
    
    return self;
}

@end




// XCFMyInfo.h
//  XCFAuthorDetail : @interface XCFAuthorDetail : NSObject <NSCoding>

#import <Foundation/Foundation.h>
@class XCFAuthorDetail;

@interface XCFMyInfo : NSObject

/**
 *  @return 快速獲取XCFAuthorDetail對象
 */
+ (XCFAuthorDetail *)info;

/**
 *  更新最新值
 *
 *  @param info 傳入的最新的對象
 */
+ (void)updateInfoWithNewInfo : (XCFAuthorDetail *)info;
@end

// XCFMyInfo.m
#import "XCFMyInfo.h"
#import "XCFAuthorDetail.h"

@implementation XCFMyInfo

static XCFAuthorDetail *_myInfo;
static NSString *const kMyInfo = @"myInfo";

+ (XCFAuthorDetail *)info
{
    // 從偏好設置中取數據
    NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:kMyInfo];
    // 解檔 另外一種方式[NSKeyedUnarchiver unarchiveObjectWithFile:]
    _myInfo = [NSKeyedUnarchiver unarchiveObjectWithData:data];
    // 判斷
    if (_myInfo == nil) {
        _myInfo = [[XCFAuthorDetail alloc] init];
        _myInfo.type        = XCFAuthorTypeMe;
        _myInfo.nfollow     = @"1";
        _myInfo.nfollowed   = @"99999";
        _myInfo.create_time = @"1970-01-01 13:10:10";
        _myInfo.ndishes     = @"520";
        _myInfo.nrecipes    = @"1314";
    }
    return _myInfo;
}

+ (void)updateInfoWithNewInfo : (XCFAuthorDetail *)info
{
    // 歸檔
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:info];
    // 存到偏好設置中
    [[NSUserDefaults standardUserDefaults] setObject:data forKey:kMyInfo];
    // 立即寫入
    [[NSUserDefaults standardUserDefaults] synchronize];
}
@end

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容