iOS 对指定Model实现自动归档

本文详细介绍了用户模型的设计,包括属性定义、单例管理、自动归档与恢复、复制功能等,采用Objective-C语言实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

.h

#import <Foundation/Foundation.h>

#import "ZJBModel.h"
#import "AutoCoding.h"

/* 某个用户 (自己/别人) */
@interface UserModel : ZJBModel <NSCoding, NSCopying>
/* 命名方式 OC/Java都是遵守驼峰原则/波浪命名方法  
   在PHP, Linux里面命名是按照 _ 隔开 全小写命名
 */
@property (nonatomic, strong) NSString *username;
@property (nonatomic, strong) NSString *password;

@property (nonatomic, strong) NSString *address;   /* 和 address 对应 */
@property (nonatomic, strong) NSString *birthday;
@property (nonatomic, strong) NSString *clientType; /* 和 client_type 对应 */
@property (nonatomic, strong) NSString *createTime; /* 和 create_time 对应 */
@property (nonatomic, strong) NSString *deviceModel;
@property (nonatomic, strong) NSString *deviceToken;
@property (nonatomic, strong) NSString *headerImg;
@property (nonatomic, strong) NSString *id;
@property (nonatomic, strong) NSString *ip;
@property (nonatomic, strong) NSString *lastLoginTime; /* last_login_time */
@property (nonatomic, assign) double lbsLat;
@property (nonatomic, assign) double lbsLong;
@property (nonatomic, strong) NSString *nickname;
@property (nonatomic, strong) NSString *osVersion;
@property (nonatomic, strong) NSString *sex;
@property (nonatomic, strong) NSString *token;
@property (nonatomic, strong) NSString *jid;


@property (nonatomic, strong) NSString *demo;

// 表示自己当前用户 单例
+ (id) currUser;

// 保存当前用户myself
+ (void) saveMyself;
// 恢复当前用户
+ (id) restoreMyself;

+ (BOOL) isExistMyself;
+ (void) deleteSaveMyself;

// 把对象self拷贝到target对象中
- (id) copyToObject:(id)target;

@end


#define myself ((UserModel *)[UserModel currUser])


.m

#import "UserModel.h"

@implementation UserModel

static UserModel *um;
+ (id) currUser {
    if (um == nil)
        um = [[UserModel alloc] init];
    return um;
}

#pragma mark - 使用AutoCoding自动进行归档

static inline NSString * myselfSaveFile() {
    return [NSHomeDirectory() stringByAppendingString:@"/Library/Caches/myself"];
}

+ (void) saveMyself {
    NSString *path = myselfSaveFile();
    // 使用自动归档开源库 直接把对象写入到文件中
    [myself writeToFile:path atomically:YES];
}

// 恢复当前用户
+ (id) restoreMyself {
    NSString *path = myselfSaveFile();
    // 从path路径找到并解档案
    UserModel *um = [self objectWithContentsOfFile:path];
    return um;
}

+ (BOOL) isExistMyself {
    NSString *path = myselfSaveFile();
    id obj = [self objectWithContentsOfFile:path];
    if ([obj isKindOfClass:[self class]]) {
        return YES;
    }
    return NO;
}

+ (void) deleteSaveMyself {
    NSString *path = myselfSaveFile();
    [[NSFileManager defaultManager] removeItemAtPath:path error:nil];
}

#pragma mark - 自动实现了拷贝NSCopying功能(NSZone申请的内存空间)

//codableProperties 获取当前对应的属性和属性值,并把相应的属性和属性值,存入到model中,实现自动归档
- (id)copyWithZone:(NSZone *)zone {
    UserModel *copy = [[[self class] allocWithZone:zone] init];
    for (NSString *key in [self codableProperties]) {
        [copy setValue:[self valueForKey:key] forKey:key];
    }
    return copy;
}

// 把self存于target对象中
- (id) copyToObject:(id)target {
    for (NSString *key in [self codableProperties]) {
        [target setValue:[self valueForKey:key] forKey:key];
    }
    return target;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值