关于注册登陆时候进行的归档解档--严焕培
#import <Foundation/Foundation.h> #import "SBModel.h" #import "AutoCoding.h" @interface UserModel : SBModel <NSCoding> /* 命名方式 OC/Java都是遵守驼峰原则/波浪命名方法 在PHP, Linux里面命名是按照 _ 隔开 全小写命名 需要把 */ //Login @property (nonatomic, strong) NSString *bid; @property (nonatomic, strong) NSString *mobile; @property (nonatomic, strong) NSString *token; // 表示自己当前用户 单例 + (id) currUser; // 保存当前用户myself + (void) saveMyself; // 恢复当前用户 + (id) restoreMyself; + (BOOL) isExistMyself; + (void) deleteSaveMyself; // 把对象self拷贝到target对象中 - (id) copyToObject:(id)target; @end //#define myself [UserModel currUser] #define myself ((UserModel *)[UserModel currUser]) #import "UserModel.h" @implementation UserModel + (id) currUser { static UserModel *um; 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功能 - (id)copyWithZone:(NSZone *)zone { id copy = [[[self class] alloc] 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; } @end
作者:SIBU iOS DEV
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.