1、为了将应用数据存储到硬盘中,iOS提供基本的文件API、Property List序列化、SQLite、CoreData以及NSCoding。对于轻量级的数据要求,NSCoding因其简单而成为一种比较合适的方式。 NSCoding是一个你需要在数据类上要实现的协议以支持数据类和数据流间的编码和解码。数据流可以持久化到硬盘。
2、是类对象本身数据的写入到本地文件。
我们需要实现两个方法: encodeWithCoder和initWithEncoder。encodeWithCoder就是编码,initWithCoder就是解码。 encodeWithCoder方法传入的是一个NSCoder对象,实现的时候我们就可以调用encodeObject、encodeFloat、 encodeInt等各种方法并通过指定键值进行编码。
APLProduct.m的代码如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
| #import "APLProduct.h"
NSString *ProductTypeDevice = @"Device"; NSString *ProductTypeDesktop = @"Desktop"; NSString *ProductTypePortable = @"Portable";
@implementation APLProduct
+ (instancetype)productWithType:(NSString *)type name:(NSString *)name year:(NSNumber *)year price:(NSNumber *)price { APLProduct *newProduct = [[self alloc] init]; newProduct.type = type; newProduct.name = name; newProduct.yearIntroduced = year; newProduct.introPrice = price; return newProduct; }
+ (NSArray *)deviceTypeNames { static NSArray *deviceTypeNames = nil; static dispatch_once_t once;
dispatch_once(&once, ^{ deviceTypeNames = @[ProductTypeDevice, ProductTypePortable, ProductTypeDesktop]; });
return deviceTypeNames; }
+ (NSString *)displayNameForType:(NSString *)type { static NSMutableDictionary *deviceTypeDisplayNamesDictionary = nil; static dispatch_once_t once;
dispatch_once(&once, ^{ deviceTypeDisplayNamesDictionary = [[NSMutableDictionary alloc] init]; for (NSString *deviceType in [self deviceTypeNames]) { NSString *displayName = NSLocalizedString(deviceType, @"dynamic"); deviceTypeDisplayNamesDictionary[deviceType] = displayName; } });
return deviceTypeDisplayNamesDictionary[type]; }
#pragma mark - Archiving
static NSString *NameKey = @"NameKey"; static NSString *TypeKey = @"TypeKey"; static NSString *YearKey = @"YearKey"; static NSString *PriceKey = @"PriceKey";
- (id)initWithCoder:(NSCoder *)aDecoder { self = [super init]; if (self) { _name = [aDecoder decodeObjectForKey:NameKey]; _type = [aDecoder decodeObjectForKey:TypeKey]; _yearIntroduced = [aDecoder decodeObjectForKey:YearKey]; _introPrice = [aDecoder decodeObjectForKey:PriceKey]; } return self; }
- (void)encodeWithCoder:(NSCoder *)aCoder { [aCoder encodeObject:self.name forKey:NameKey]; [aCoder encodeObject:self.type forKey:TypeKey]; [aCoder encodeObject:self.yearIntroduced forKey:YearKey]; [aCoder encodeObject:self.introPrice forKey:PriceKey]; }
@end
|
APLProduct.h的代码如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| extern NSString *ProductTypeDevice; extern NSString *ProductTypeDesktop; extern NSString *ProductTypePortable;
@interface APLProduct : NSObject <NSCoding>
@property (nonatomic, copy) NSString *name; @property (nonatomic, copy) NSString *type; @property (nonatomic, copy) NSNumber *yearIntroduced; @property (nonatomic) NSNumber *introPrice;
+ (instancetype)productWithType:(NSString *)type name:(NSString *)name year:(NSNumber *)year price:(NSNumber *)price;
+ (NSArray *)deviceTypeNames; + (NSString *)displayNameForType:(NSString *)type;
@end
|