IOS学习笔记二十三对象归档(NSKeyedArchiver、NSKeyedUnArchiver、NSCodeing)

1、NSKeyedArchiver、NSKeyedUnArchiver

1)、archiveRootObject:toFile 归档对象到这个路径文件

2)、unarchiveObjectWithFile:从这个路径文件把对象进行恢复

对象归档这里我们可以理解Android里面的序列化,就是把对象保存到文件持久化,Android里面进行持久化的必须实现SerializableParcelable,然后IOS里面持久化必须实现NSCodeing协议,IOS进行持久化操作一般需要NSKeyedArchiver实现

 

2、NSCodeing协议

1)、initWithCoder:该方法恢复对象

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目录下创建一个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进行恢复对象
        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,发现是二进制文件
//        file chenyu.txt
//        chenyu.txt: Apple binary property list
        //下面为chenyu.txt文件的内容,我是用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>©²ÄÇÌ^@^@^@^@^@^@^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、运行结果

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、问题思考

是不是每次只能保持一个对象到文件里面呢?暂时感觉是这样的,后面遇到问题再分析。

猜你喜欢

转载自blog.csdn.net/u011068702/article/details/81150850