1. 程式人生 > >iOS NSKeyedArchiver序列化 儲存陣列物件

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