데이터 저장소 - 아카이브 아카이빙

3571 단어

1. 소개

                   。
  :         。
  :        。

2. 참고:

        ,      。

3. 압축 파일 대상의 쓰기

1.      NSCoding   

@interface Student : NSObject

@property(nonatomic,copy)NSString *name;

@property(nonatomic,assign) CGFloat score;

@property(nonatomic,assign) BOOL sex;

@end
//1.    
/**
 *             
 *
 *  @param aCoder    
 */
- (void)encodeWithCoder:(NSCoder *)aCoder{
    [aCoder encodeObject:self.name forKey:@"name"];
    [aCoder encodeFloat:self.score forKey:@"score"];
    [aCoder encodeBool:self.sex forKey:@"sex"];
}

/**
 *                    
 *
 *  @param aDecoder    
 *
 *  @return   
 */
- (instancetype)initWithCoder:(NSCoder *)aDecoder{
    if (self = [super init]) {
        self.name = [aDecoder decodeObjectForKey:@"name"];
        self.score = [aDecoder decodeFloatForKey:@"score"];
        self.sex = [aDecoder decodeBoolForKey:@"sex"];
    }
    return self;
}


//1.    
- (void)encodeWithCoder:(NSCoder *)aCoder{
    //            
    [super encodeWithCoder: aCoder];
    [aCoder encodeObject:self.name forKey:@"name"];
    [aCoder encodeFloat:self.score forKey:@"score"];
    [aCoder encodeBool:self.sex forKey:@"sex"];
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder{
    //            
    if (self = [super initWithCoder: aDecoder]) {
        self.name = [aDecoder decodeObjectForKey:@"name"];
        self.score = [aDecoder decodeFloatForKey:@"score"];
        self.sex = [aDecoder decodeBoolForKey:@"sex"];
    }
    return self;
}

4. 아카이빙 - NSKeyedArchiver

//      
- (void)save{
    //1.    
    Student *stu = [[Student alloc] init];
    stu.name = @"zhangsan";
    stu.score = 90;
    stu.sex = YES;
    //2.        
    NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    filePath = [filePath stringByAppendingPathComponent:@"student.data"];
    //3.  
    [NSKeyedArchiver archiveRootObject:stu toFile:filePath];
}

//      
- (void)save{
    //1.    
    Student *stu = [[Student alloc] initWithName:@"zhangsan" withScore:90 withSex:YES];
    
    Student *stu1 = [[Student alloc] initWithName:@"lisi" withScore:90 withSex:YES];
    
    NSArray *arr = @[stu,stu1];
    //2.        
    NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    filePath = [filePath stringByAppendingPathComponent:@"student.data"];
    //3.  
    [NSKeyedArchiver archiveRootObject:arr toFile:filePath];
}

5. 파일 해제 -- NSKeyedUnarchiver

//      
- (void)getData{
    //1.        
    NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    filePath = [filePath stringByAppendingPathComponent:@"student.data"];
    //2.  
    Student *stu = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
}

//      
- (void)getData{
    //2.        
    NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    filePath = [filePath stringByAppendingPathComponent:@"student.data"];
    //3.  
    NSArray *stus = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
    NSLog(@"%@",stus);
}

좋은 웹페이지 즐겨찾기