利用NSKeyedUnarchiver把数据存储到本地

在开发中,如果使用plist存贮到本地的话,存贮的类型有限,这时候,我们可以就可以考虑利用NSKeyedUnarchiver把数据存储到本地,通过这个方法我们可以将model存贮到本地,一般用来保存用户的账号信息之类的

第一步,我们建一个model,

#import <Foundation/Foundation.h>

@interface Account : NSObject

@property (nonatomic, copy) NSString *phone; //手机号

@property (nonatomic, copy) NSString *uid; //用户id

@property (nonatomic, copy) NSString *nickname;

@property (nonatomic, copy) NSString *password;

@property (nonatomic, copy) NSString *tel; //座机

@property (nonatomic, copy) NSString *last_login;

//@property (nonatomic, copy) NSString *new_phone;

@property (nonatomic, copy) NSString *uthumb; //头像地址

@property (nonatomic, copy) NSString *province;

@property (nonatomic, copy) NSString *city;

@property (nonatomic, copy) NSString *area;

@property (nonatomic, copy) NSData *photoData;

@end

#import "Account.h"

@implementation Account

  • (NSDictionary *)replacedKeyFromPropertyName

{

return @{@"uid": @"id"};

}

@end

//定义一个 .h 头文件,用来简写我们的单例

// .h

#define single_interface(class) + (class *)shared##class;

// \ 代表下一行也属于宏

// ## 是分隔符

#define single_implementation(class) \

static class *_instance; \

\

  • (class *)shared##class \

{ \

if (_instance == nil) { \

_instance = \[self alloc init]; \

} \

return _instance; \

} \

\

  • (id)allocWithZone:(NSZone *)zone \

{ \

static dispatch_once_t onceToken; \

dispatch_once(&onceToken, ^{ \

_instance = super allocWithZone:zone; \

}); \

return _instance; \

}
第二步:封装一个方法,使用NSKeyedUnarchiver保存到本地
// AccountTool.h

//

#import <Foundation/Foundation.h>

#import "Account.h"

#import "SingleTon.h"

@interface AccountTool : NSObject

single_interface(AccountTool)

  • (void)saveAccount:(Account *)account;

  • (void)removeAccount;

// 获得当前账号

@property (nonatomic, readonly) Account *account;

@end

//

// AccountTool.m

//

#import "AccountTool.h"

// 文件路径---->>获取分类的沙盒文件路径

#define kFile NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)\[0 stringByAppendingPathComponent:@"account.data"]

@implementation AccountTool

single_implementation(AccountTool)

  • (instancetype)init

{

if (self = super init) {

//解归档该路径下的文件

_account = NSKeyedUnarchiver unarchiveObjectWithFile:kFile;

}

return self;

}

  • (void)saveAccount:(Account *)account

{

_account = account;

//将数据归档,如果前面的文件不存在,就会在归档数据时创建

NSKeyedArchiver archiveRootObject:account toFile:kFile;

}

  • (void)removeAccount

{

if (\[NSFileManager defaultManager fileExistsAtPath:kFile]) {

\[NSFileManager defaultManagerremoveItemAtPath:kFile error:nil];

}

}

@end

//这样我们就可以使用这个工具类了(下面以保存登录后的账号为例说明如何使用)

判断是否有数据

if (AccountTool sharedAccountTool.account.uid == nil || ! kUserLogin || \[AccountTool sharedAccountTool.account.uid isKindOfClass:NSNull class]){

//无数据

}else {

//有数据

}

//登录成功后将数据用mj工具类将字典直接保存到model中,然后直接保存到本地

Account *currentAccount = Account mj_objectWithKeyValues:userInfo;

\[AccountTool sharedAccountTool saveAccount:currentAccount];
//

相关推荐
00后程序员6 小时前
iOS 实时活动怎么开发,Windows 上用 KXBuild 和 ActivityKit 写灵动岛
后端·ios
iReaShare7 小时前
如何在没有 Wi-Fi 的情况下备份 iPhone
ios
CocoaKier1 天前
苹果开发者注意,请在2027年1月31日前完成合规验证,否则中国区可能下架
ios·aigc·apple
二流小码农1 天前
鸿蒙开发:证书只能创建3个,第4个应用怎么办
android·ios·harmonyos
bcbnb1 天前
Windows 上 iOS 开发接 CI,KXBuild 自托管 runner 出签名 ipa
后端·ios
疯华整猫2 天前
别再抄 Flutter 了:2026 年 Android / iOS / 小程序跨端架构选型的四本成本账
android·前端·ios
黑科技iOS上架2 天前
iOS游戏换皮上架困境:4.3同质化拒审
人工智能·ios·审核
黑科技iOS上架2 天前
iOS二次开发应用上架难题:4.3a同质化拒审
ios·审核
传奇开心果编程2 天前
【Flutter踩坑排雷】第30课:异常未捕获坑|未捕获异常直接闪退,全局兜底故障处理
android·flutter·ios·移动开发·dart·ui框架·声明式ui
茶底世界之下2 天前
海边游乐场的黄昏:用 Awaking 留住一层暖光
ios·app·产品