IOS學習筆記二十三對象歸檔(NSKeyedArchiver、NSKeyedUnArchiver、NSCodeing)
1、NSKeyedArchiver、NSKeyedUnArchiver
1)、archiveRootObject:toFile 歸檔對象到這個路徑文件
2)、unarchiveObjectWithFile:從這個路徑文件把對象進行恢復(fù)
對象歸檔這里我們可以理解Android里面的序列化,就是把對象保存到文件持久化,Android里面進行持久化的必須實現(xiàn)Serializable和Parcelable,然后IOS里面持久化必須實現(xiàn)NSCodeing協(xié)議,IOS進行持久化操作一般需要NSKeyedArchiver實現(xiàn)。
2、NSCodeing協(xié)議
1)、initWithCoder:該方法恢復(fù)對象
2)、encodeWithCoder:歸檔該對象
3、測試Demo(把Dictionary和普通對象進行對象歸檔)
IApple.h
#import <Foundation/Foundation.h>
#ifndef IApple_h
#define IApple_h
@interface IApple : NSObject <NSCoding>
@property (nonatomic, copy) NSString *color;
@property (nonatomic, assign) double weight;
@property (nonatomic, assign) int size;
-(id)initWithColor:(NSString *) color weight:(double) weight size:(int) size;
@end
#endif /* IApple_h */
IApple.m
#import "IApple.h"
#import <Foundation/Foundation.h>
@implementation IApple
@synthesize color = _color;
@synthesize weight = _weight;
@synthesize size = _size;
-(id)initWithColor:(NSString *) color weight:(double) weight size:(int) size
{
if (self = [super init])
{
self.color = color;
self.weight = weight;
self.size = size;
}
return self;
}
-(NSString *)description
{
return [NSString stringWithFormat:@"<IApple [color = %@, weight = %g, _size = %d]>", self.color, self.weight, self.size];
}
-(void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:_color forKey:@"color"];
[aCoder encodeDouble:_weight forKey:@"weight"];
[aCoder encodeInt:_size forKey:@"size"];
}
-(id) initWithCoder:(NSCoder *)aDecoder
{
_color = [aDecoder decodeObjectForKey:@"color"];
_weight = [aDecoder decodeDoubleForKey:@"weight"];
_size = [aDecoder decodeIntForKey:@"size"];
return self;
}
@end
main.m
#import "IApple.h"
int main(int argc, char * argv[]) {
@autoreleasepool {
//在document目錄下創(chuàng)建一個chenyu.txt空文件
NSArray *docPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [docPaths objectAtIndex:0];
NSLog(@"document path:%@", path);
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *chenyuPath = [path stringByAppendingPathComponent:@"chenyu.txt"];
BOOL isSuccess = [fileManager createFileAtPath:chenyuPath contents:nil attributes:nil];
if (isSuccess) {
NSLog(@"make chenyu.txt success");
} else {
NSLog(@"make chenyu.txt fail");
}
//用NSKeyedArchiver進行對象歸檔
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:10], @"oc", [NSNumber numberWithInt:20], @"java", [NSNumber numberWithInt:30], @"c++", nil];
[NSKeyedArchiver archiveRootObject:dic toFile:chenyuPath];
NSLog(@"123");
//利用NSKeyedUnarchiver進行恢復(fù)對象
NSDictionary *dic1 = [NSKeyedUnarchiver unarchiveObjectWithFile:chenyuPath];
NSLog(@"oc value is %@", [dic1 valueForKey:@"oc"]);
NSLog(@"java value is %@", [dic1 valueForKey:@"java"]);
NSLog(@"c++ value is %@", [dic1 valueForKey:@"c++"]);
//我們先看下chenyu.txt是什么文件,我們用file命令看下 file chenyu.txt,發(fā)現(xiàn)是二進制文件
// file chenyu.txt
// chenyu.txt: Apple binary property list
//下面為chenyu.txt文件的內(nèi)容,我是用vim打開的
// bplist00?^A^B^C^D^E^F^Z^[X$versionX$objectsY$archiverT$top^R^@^A<86> ¥^G^H^Q^R^SU$nulló
// ^K^L^N^PWNS.keysZNS.objectsV$class?^M<80>^B?^O<80>^C<80>^DRoc^P ò^T^U^V^WZ$classnameX$classes\NSDictionary¢^X^Y\NSDictionaryXNSObject_^P^ONSKeyedArchiver?^\^]Troot<80>^A^H^Q^Z#-27=CJR]dfhjlnqsx<83><8c><99><9c>?2??ì^@^@^@^@^@^@^A^A^@^@^@^@^@^@^@^^^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@?
//我們再用chenyu.txt歸檔IApple對象
IApple *apple = [[IApple alloc] initWithColor:@"red" weight:5.6 size:20];
IApple *apple1 = [[IApple alloc] initWithColor:@"wihte" weight:6.6 size:30];
//用NSKeyedArchiver進行IApple對象歸檔
[NSKeyedArchiver archiveRootObject:apple toFile:chenyuPath];
[NSKeyedArchiver archiveRootObject:apple1 toFile:chenyuPath];
IApple *saveApple = [NSKeyedUnarchiver unarchiveObjectWithFile:chenyuPath];
NSLog(@"saveApple is %@", saveApple);
}
}
4、運行結(jié)果
2018-07-22 00:08:57.804831+0800 cyTest[37026:15851965] document path:/Users/ls/Library/Developer/CoreSimulator/Devices/3FF9B833-FAF8-4C30-A855-3D40A4EAE8A6/data/Containers/Data/Application/272166E9-67BC-4E6B-B79A-0FF9DA389D7D/Documents
2018-07-22 00:08:57.810379+0800 cyTest[37026:15851965] make chenyu.txt success
2018-07-22 00:08:57.813994+0800 cyTest[37026:15851965] 123
2018-07-22 00:08:57.815001+0800 cyTest[37026:15851965] oc value is 10
2018-07-22 00:08:57.815228+0800 cyTest[37026:15851965] java value is 20
2018-07-22 00:08:57.815438+0800 cyTest[37026:15851965] c++ value is 30
2018-07-22 00:08:57.822014+0800 cyTest[37026:15851965] saveApple is <IApple [color = wihte, weight = 6.6, _size = 30]>
5、問題思考
是不是每次只能保持一個對象到文件里面呢?暫時感覺是這樣的,后面遇到問題再分析。
作者:chen.yu
深信服三年半工作經(jīng)驗,目前就職游戲廠商,希望能和大家交流和學習,
微信公眾號:編程入門到禿頭 或掃描下面二維碼
零基礎(chǔ)入門進階人工智能(鏈接)