iOS NSKeyedArchiver序列化 存储数组对象

@interface ARWGestureModel : NSObject<NSCopying,NSCoding>
@property(nonatomic,strong)NSString*name;
@property(nonatomic,strong)NSString *age;
@end

#import "ARWGestureModel.h"

@implementation ARWGestureModel
@synthesize name;
@synthesize age;
#pragma nscoding
-(void)encodeWithCoder:(NSCoder *)aCoder{
    [aCoder encodeObject:name forKey:@"name"];
    [aCoder encodeObject:age forKey:@"age"];
    [aCoder encodeObject:array forKey:@"array"];
}
-(instancetype)initWithCoder:(NSCoder *)aDecoder{
    if (self = [super init]) {
        self.name = [aDecoder decodeObjectForKey:@"name"];
        self.age = [aDecoder decodeObjectForKey:@"age"];
    }
    return self;
}
#pragma nscoping
-(id)copyWithZone:(NSZone *)zone{
    ARWGestureModel *copymodel = [[self.class allocWithZone:zone]init];
    copymodel.name = [self.name copyWithZone:zone];
    copymodel.age = [self.age copyWithZone:zone];
    return copymodel;
}
@end
//
//  Test1ViewController.m
//  Test
//
//  Created by BOB on 17/4/7.
//  Copyright © 2017年 BOB. All rights reserved.
//

#import "Test1ViewController.h"
#import "ARWGestureModel.h"

@interface Test1ViewController ()
@property(nonatomic,strong)NSMutableArray *arr;
@end

@implementation Test1ViewController
-(NSMutableArray *)arr{
    if (!_arr) {
        _arr = [NSMutableArray new];
        for (int i=0; i<8; i++) {
            ARWGestureModel *model = [[ARWGestureModel alloc]init];
            model.name = [NSString stringWithFormat:@"bob%d",i];
            model.age = [NSString stringWithFormat:@"%d",i];
        [_arr addObject:model];
        }
    }
    return _arr;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    UIButton *writeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:writeBtn];
    [writeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(100);
        make.top.mas_equalTo(100);
        make.centerX.mas_equalTo(self.view.mas_centerX);

    }];
    writeBtn.backgroundColor = [UIColor blueColor];
    [writeBtn setTitle:@"存储" forState:UIControlStateNormal];
    [writeBtn addTarget:self action:@selector(write:) forControlEvents:UIControlEventTouchUpInside];

    UIButton *readBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:readBtn];
    [readBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(100);
        make.top.mas_equalTo(300);
        make.centerX.mas_equalTo(self.view.mas_centerX);

    }];
    readBtn.backgroundColor = [UIColor blueColor];
    [readBtn setTitle:@"存储" forState:UIControlStateNormal];
    [readBtn addTarget:self action:@selector(read:) forControlEvents:UIControlEventTouchUpInside];

    // Do any additional setup after loading the view.
}
-(void)write:(id)sender{
    NSData *wData = [NSKeyedArchiver archivedDataWithRootObject:self.arr];
    [[NSUserDefaults standardUserDefaults]setObject:wData forKey:@"STORAGE_GESTURE_MODEL_LIST"];
    [[NSUserDefaults standardUserDefaults]synchronize];
}
-(void)read:(id)sender{
    NSData *data = [[NSUserDefaults standardUserDefaults]objectForKey:@"STORAGE_GESTURE_MODEL_LIST"];
    NSArray *arrt = [NSKeyedUnarchiver unarchiveObjectWithData:data];
    for (ARWGestureModel* xx in arrt) {
        NSLog(@"%@",xx);
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值