美文网首页
NSKeyedArchiver序列化数组对象

NSKeyedArchiver序列化数组对象

作者: Fisher123 | 来源:发表于2018-12-01 16:31 被阅读0次

新建一个Model,命名LBUserInfo,需要实现NSCopying和NSCoding协议,LBUserInfo.h文件内容如下:

//
//  LBUserInfo.h
//  ArchiveTest
//
//  Created by liyu on 2018/11/29.
//  Copyright © 2018 liyu. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface LBUserInfo : NSObject<NSCopying, NSCoding>
// 用户名
@property (nonatomic, copy) NSString *userName;
// 密码
@property (nonatomic, copy) NSString *userPassword;
// urlString
@property (nonatomic, copy) NSString *urlString;
// 登录状态
@property (nonatomic, copy) NSString *loginState;

+ (LBUserInfo *)shareInstance;

@end

LBUserInfo.m文件内容如下:

//
//  LBUserInfo.m
//  ArchiveTest
//
//  Created by liyu on 2018/11/29.
//  Copyright © 2018 liyu. All rights reserved.
//

#import "LBUserInfo.h"

@implementation LBUserInfo

LBUserInfo *instance = nil;
+ (LBUserInfo *)shareInstance {
    if (instance == nil) {
        instance = [[LBUserInfo alloc] init];
    }
    return instance;
}

#pragma mark - NSCoding
- (void)encodeWithCoder:(NSCoder *)aCoder {
    [aCoder encodeObject:self.userName forKey:@"userName"];
    [aCoder encodeObject:self.userPassword forKey:@"userPassword"];
    [aCoder encodeObject:self.urlString forKey:@"urlString"];
    [aCoder encodeObject:self.loginState forKey:@"loginState"];
}

- (id)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super init]) {
        self.userName = [aDecoder decodeObjectForKey:@"userName"];
        self.userPassword = [aDecoder decodeObjectForKey:@"userPassword"];
        self.urlString = [aDecoder decodeObjectForKey:@"urlString"];
        self.loginState = [aDecoder decodeObjectForKey:@"loginState"];
    }
    return self;
}

#pragma mark - NSCopying
- (id)copyWithZone:(NSZone *)zone {
    LBUserInfo *model = [[self.class allocWithZone:zone] init];
    model.userName = [self.userName copyWithZone:zone];
    model.userPassword = [self.userPassword copyWithZone:zone];
    model.urlString = [self.urlString copyWithZone:zone];
    model.loginState = [self.loginState copyWithZone:zone];
    return model;
}

@end

固化数据:

NSMutableArray *userInfoArray = [NSMutableArray array];

 LBUserInfo *model = [[LBUserInfo alloc] init];
 model.userName = self.userName;
 model.userPassword = self.userPassword;
 model.urlString = data;
 model.loginState = @"1";
 [userInfoArray addObject:model];
 
 // 固化数据到本地
 NSData *newUserInfoData = [NSKeyedArchiver archivedDataWithRootObject:userInfoArray];
 [[NSUserDefaults standardUserDefaults] setObject:newUserInfoData forKey:@"userInfoData"];
 [[NSUserDefaults standardUserDefaults] synchronize];

读取数据:

  // 反序列化存储的用户信息
  NSData *userInfoData = [[NSUserDefaults standardUserDefaults] objectForKey:@"userInfoData"];
  NSMutableArray *userInfoArray = [NSKeyedUnarchiver unarchiveObjectWithData:userInfoData];
  
  for (LBUserInfo *model in userInfoArray) {
            NSLog(@"userName = %@", model.userName);
            NSLog(@"userPassword = %@", model.userPassword);
            NSLog(@"urlString = %@", model.urlString);
            NSLog(@"loginState = %@", model.loginState);
        }

相关文章

网友评论

      本文标题:NSKeyedArchiver序列化数组对象

      本文链接:https://www.haomeiwen.com/subject/aizfcqtx.html