利用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];
//

相关推荐
大熊猫侯佩5 小时前
WWDC26 SwiftUI 进化之路:砸碎黑盒,彻底迎来开发自由!
ios·swiftui·swift
游戏开发爱好者86 小时前
iPhone真机调试有哪些方法?一次定位推送权限问题时整理出来的几种方案
ide·vscode·ios·objective-c·个人开发·swift·敏捷流程
大熊猫侯佩12 小时前
WWDC26 最被忽视的王炸:告别“伪并发”陷阱,Swift 6.4 的 async defer
ios·swift·编程语言
h-189-53-67120713 小时前
苹果开发者账号防关联3.2f隔离环境传包提审iOS开发上架的高效隔离方案:iOSUploader工具实用解析
ios·ios上架·ios审核·苹果审核·苹果开发者账号·苹果开发者封号
Legendary_00816 小时前
LDR6020P:iPad 一体式皮套键盘 OTG 应用的核心引擎
ios·计算机外设·ipad
Digitally1 天前
如何高效地将文件从电脑传输到 iPad:6 种简单方法
ios·电脑·ipad
萤萤七悬1 天前
【Python笔记】AI帮封装Airtest IOS-WDA touch操作时的factor坐标转换
笔记·python·ios
库奇噜啦呼2 天前
【iOS】源码学习-锁的原理
学习·ios·cocoa
Digitally2 天前
如何通过蓝牙将 iPhone 文件传输到电脑?5 种替代方案
ios·电脑·iphone