< Objective-C >文件操作-归档

系统类型对象归档:已经实现了NSCoding协议,支持归档

写入
-writeToFile:atomically;

读取
-dictionaryWithContentsOfFile;
-arrayWithContentsOfFile;
-dataWithContentsOfFile;
-stringWithContentsOfFile;

        //字符串对象归档以及解归档
        NSString *str = @"Hello World!";
        [str writeToFile:@"str.txt" atomically:YES encoding:NSUTF8StringEncoding error:nil];
        
        NSString *reStr = [NSString stringWithContentsOfFile:@"str.txt" encoding:NSUTF8StringEncoding error:nil];
        NSLog(@"%@",reStr);
        
        //数组对象归档以及解归档
        NSArray *arr = @[@"1",@"2",@"3"];
        [arr writeToFile:@"arr.plist" atomically:YES];
        
        NSArray *reArr = [NSArray arrayWithContentsOfFile:@"arr.plist"];
        NSLog(@"%@",reArr);
        
        //字典对象归档以及解归档
        NSDictionary *dic = @{@"1": @"one",
                              @"2": @"two",
                              @"3": @"three"};
        [dic writeToFile:@"dic.plist" atomically:YES];
        
        NSDictionary *reDic = [NSDictionary dictionaryWithContentsOfFile:@"dic.plist"];
        NSLog(@"%@",reDic);

plist:属性列表文件

自定义类型对象归档:实现NSCoding协议

NSCoding协议:
-(void)encodeWithCoder:(NSCoder *)encoder
-(id)initWithCoder:(NSCoder *)decoder

NSCoder类:
encode方法:
-(void)encodeBool:(BOOL)boolv forKey:(NSString *)key;
-(void)encodeObject:(id)objv forKey:(NSString *)key;

decode方法:
-(BOOL)decodeBoolForKey:(NSString *)key;
-(id)decodeObjectForKey:(NSString *)key;

代码

Grade.h

@interface Grade : NSObject <NSCoding>

@property(copy, nonatomic) NSString *name;
@property(nonatomic) int score;

-(id)initWithName:(NSString *)name andScore:(int)score;

@end

Grade.m

@implementation Grade

-(id)initWithName:(NSString *)name andScore:(int)score {
    self = [super init];
    self.name = name;
    self.score = score;
    return self;
}

-(NSString *)description {
    NSString *str = [NSString stringWithFormat:@"[%@,%d]",self.name,self.score];
    return str;
}

-(void)encodeWithCoder:(NSCoder *)aCoder {
    [aCoder encodeObject:self.name forKey:@"Name"];
    [aCoder encodeInt:self.score forKey:@"Score"];
}

-(id)initWithCoder:(NSCoder *)aDecoder {
    self = [super init];
    if(self) {
        self.name = [aDecoder decodeObjectForKey:@"Name"];
        self.score = [aDecoder decodeIntForKey:@"Score"];
    }
    return self;
}

@end

main.m

        //自定义对象的归档
        Grade *g1 = [[Grade alloc]initWithName:@"mary" andScore:50];
        Grade *g2 = [[Grade alloc]initWithName:@"mike" andScore:70];
        Grade *g3 = [[Grade alloc]initWithName:@"jane" andScore:85];
        
        NSMutableArray *gradeList = [NSMutableArray array];
        [gradeList addObject:g1];
        [gradeList addObject:g2];
        [gradeList addObject:g3];
        
        [NSKeyedArchiver archiveRootObject:gradeList toFile:@"gradeList.plist"];
        
        
        //自定义对象的反归档
        NSMutableArray *readList = [NSKeyedUnarchiver unarchiveObjectWithFile:@"gradeList.plist"];
        NSLog(@"%@",readList);
原文地址:https://www.cnblogs.com/aY-Wonder/p/4563804.html