iOS 数据持久化之归档序列化存储

归档可以直接将对象存储为文件,也可将文件解档为对象。归档后的文件是加密的,相当安全。

 

归档方法:

一、在需要归档的类中遵循归档协议,遵守NSCoding或者NSSecureCoding协议。如果采用NSSecureCoding协议,必须重写supportsSecureCoding 方法并返回 YES

#import 

@interface UserModel : NSObject

//需要归档的属性
@property (nonatomic,copy) NSString *code_number;//工号

@property (nonatomic,copy) NSString *department_name;//所属部门

@property (nonatomic,copy) NSString *id;//ID

@property (nonatomic,copy) NSString *logo;//头像

@property (nonatomic,copy) NSString *position_name;//职位

@property (nonatomic,copy) NSString *username;//名字

@property (nonatomic,copy) NSString *vacation;//假期

@end

 

二、在归档对象的 .m 方法中实现

#import "UserModel.h"

@implementation UserModel

- (void)encodeWithCoder:(nonnull NSCoder *)aCoder {
    //告诉系统归档的属性是哪些
    [aCoder encodeObject:_code_number forKey:@"userCodeNum"];
    [aCoder encodeObject:_logo forKey:@"userLogo"];
    [aCoder encodeObject:_id forKey:@"userID"];
    [aCoder encodeObject:_department_name forKey:@"department"];
    [aCoder encodeObject:_position_name forKey:@"position"];
    [aCoder encodeObject:_username forKey:@"username"];
    [aCoder encodeObject:_vacation forKey:@"vacation"];
}

- (nullable instancetype)initWithCoder:(nonnull NSCoder *)aDecoder {
    
    if (self = [super init]) {
        //解档
        self.code_number =  [aDecoder decodeObjectForKey:@"userCodeNum"];
        self.logo = [aDecoder decodeObjectForKey:@"userLogo"];
        self.id = [aDecoder decodeObjectForKey:@"userID"];
        self.department_name = [aDecoder decodeObjectForKey:@"department"];
        self.position_name = [aDecoder decodeObjectForKey:@"position"];
        self.username = [aDecoder decodeObjectForKey:@"username"];
        self.vacation = [aDecoder decodeObjectForKey:@"vacation"];
    }
    
    return self;
}

+ (BOOL)supportsSecureCoding {
    return YES;
}


@end

 

三、归档方法的实现(在Controller中实现具体的存取操作)

//归档用户资料
- (void)saveUserInfo{
    
    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"userInfo.plist"];
    
    if (!self.userModel) {
        return;
    }
    
    if (@available(iOS 11.0, *)) {
        NSError *error;

        //会调用对象的 encodeWithCoder 方法
        NSData *data = [NSKeyedArchiver archivedDataWithRootObject:self.userModel requiringSecureCoding:YES error:&error];
        
        if (error) {
            return;
        }
        
        [data writeToFile:path atomically:YES];
    } else {
        // Fallback on earlier versions
        [NSKeyedArchiver archiveRootObject:_userModel toFile:path];
    }
    
    //    Log(@"%@",path);
}

 

四、解归档方法(在Controller中实现具体的存取操作)

/**
 解归档用户资料
 */
- (UserModel *)obtainUserInfo
{
    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"userInfo.plist"];
    if (@available(iOS 11.0, *)) {
        NSError *error;
        NSData *data = [[NSData alloc] initWithContentsOfFile:path];
        _userModel = [NSKeyedUnarchiver unarchivedObjectOfClass:[UserModel class] fromData:data error:&error];
    }else {
        // Fallback on earlier versions
        _userModel = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
    }
    
    if (!_userModel) {
        return nil;
    }

    return _userModel;
}

 

五、删除用户信息

//删除用户信息
- (void)removeUserInfo{
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"userInfo.plist"];
    BOOL remove = [fileManager removeItemAtPath:path error:nil];
    
    if (remove == YES) {
        [Helper sharedInstance].userModel = nil;
    }
}

 

你可能感兴趣的:(iOS 数据持久化之归档序列化存储)