天天看点

iOS 归档 和 自定义归档

自定义归档

<span style="font-size:12px;">#pragma mark  自定义归档
-(void)custom
{
    User *user = [[User alloc] init];
    user.name = @"gaobo";
    
    NSString *home = NSHomeDirectory();
    NSString *path = [home stringByAppendingPathComponent:@"custom.info"];
    BOOL success = [NSKeyedArchiver archiveRootObject:user toFile:path];
    if (success) {
        NSLog(@"自定义归档成功");
    }else{
        NSLog(@"自定义归档失败");
    }
}</span>
           
<span style="font-size:12px;">#pragma mark  自定义解归档
-(void)readUserInfo
{
    NSString *home = NSHomeDirectory();
    NSString *path = [home stringByAppendingPathComponent:@"custom.info"];
    
    User *user = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
    
    if (user.name) {
        NSLog(@"%@",user.name);
    }else{
        NSLog(@"没值");
    }

}</span>