@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
#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];
}
-(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];
}
@end