// // StudentObject.h // NSObject序列化 // // Created by goscam on 15/12/22. // Copyright © 2015年 goscam. All rights reserved. // #import <Foundation/Foundation.h> @interface peopleObject:NSObject<NSCopying,NSCoding> @property(nonatomic,copy)NSString *num; @end @interface StudentObject : NSObject<NSCopying,NSMutableCopying,NSCoding> @property(nonatomic,strong)NSMutableArray *itemList; @property(nonatomic,copy)NSString *name; @property(nonatomic,assign)NSString *title; @property(nonatomic,copy)NSString *add; @end
// // StudentObject.m // NSObject序列化 // // Created by goscam on 15/12/22. // Copyright © 2015年 goscam. All rights reserved. // #import "StudentObject.h" #import "NSArray+SNFoundation.h" @implementation peopleObject - (void)encodeWithCoder:(NSCoder *)aCoder { [aCoder encodeObject:self.num forKey:@"num"]; } - (id)initWithCoder:(NSCoder *)coder { self = [super init]; if (self) { self.num = [coder decodeObjectForKey:@"num"]; } return self; } @end @implementation StudentObject - (void)encodeWithCoder:(NSCoder *)aCoder { NSData *listData = [NSKeyedArchiver archivedDataWithRootObject:self.itemList]; [aCoder encodeObject:listData forKey:@"list"]; [aCoder encodeObject:self.name forKey:@"name"]; [aCoder encodeObject:self.title forKey:@"title"]; [aCoder encodeObject:self.add forKey:@"add"]; } - (id)initWithCoder:(NSCoder *)coder { self = [super init]; if (self) { NSData *listData = [coder decodeObjectForKey:@"list"]; self.itemList = [NSKeyedUnarchiver unarchiveObjectWithData:listData]; self.name = [coder decodeObjectForKey:@"name"]; self.title = [coder decodeObjectForKey:@"title"]; self.add = [coder decodeObjectForKey:@"add"]; } return self; } //深拷贝 -(id) mutableCopyWithZone : (NSZone *) zone { StudentObject *dto = [[StudentObject allocWithZone : zone] init]; dto.itemList = [self.itemList trueDeepMutableCopy]; dto.name = [self.name copy]; dto.title = [self.title copy]; dto.add = [self.add copy]; return dto; } - (id)copyWithZone:(NSZone *)zone{ StudentObject *dto = [[[self class] allocWithZone:zone] init]; dto.itemList = [self.itemList trueDeepMutableCopy]; dto.name = [self.name copy]; dto.title = [self.title copy]; dto.add = [self.add copy]; return dto; } @end
// // TMCacheExtend.h // IBY // // Created by panshiyu on 15/5/8. // Copyright (c) 2015年 com.biyao. All rights reserved. // #import <Foundation/Foundation.h> #import "TMCache.h" @interface TMCache (Extension) + (instancetype)TemporaryCache; + (instancetype)PermanentCache; @end
// // TMCacheExtend.m // IBY // // Created by panshiyu on 15/5/8. // Copyright (c) 2015年 com.biyao. All rights reserved. // #import "TMCacheExtend.h" #define kTemporaryCache @"com.dv.cache.temporary" #define kPermanentCache @"com.dv.cache.permanentCache" @implementation TMCache (Extension) + (instancetype)TemporaryCache{ return [[TMCache sharedCache] initWithName:kTemporaryCache]; } + (instancetype)PermanentCache { return [[TMCache sharedCache] initWithName:kPermanentCache]; } @end
- (IBAction)write:(id)sender { NSMutableArray *array = [[NSMutableArray alloc]init]; StudentObject *object = [[StudentObject alloc]init]; object.name = @"hello"; object.add = @"wuhan"; object.title = @"write"; for (int i = 0; i < 100; i++) { peopleObject *people = [[peopleObject alloc]init]; people.num = [NSString stringWithFormat:@"%d",i]; [array addObject:people]; } object.itemList = array ; NSString *file = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"timerSwitch.data"]; [NSKeyedArchiver archiveRootObject:[NSKeyedArchiver archivedDataWithRootObject:object] toFile:file]; _stuObject = object; NSLog(@"写入成功"); [[TMCache TemporaryCache] setObject:object forKey:@"cache"]; }
- (IBAction)read:(id)sender { NSLog(@"ddd"); NSString *file = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"timerSwitch.data"]; NSData* data = [NSKeyedUnarchiver unarchiveObjectWithFile:file]; if (data) { StudentObject *stuobject = [NSKeyedUnarchiver unarchiveObjectWithData:data]; [_object.itemList removeObjectAtIndex:0]; [_object.itemList removeObjectAtIndex:3]; [_object.itemList removeObjectAtIndex:5]; } StudentObject *stu = [[TMCache TemporaryCache] objectForKey:@"cache"]; }
浙公网安备 33010602011771号