【开发工具】YTKKeyValueStore 键值存储--存储自定义类型

1,使用 'MJExtension' 处理字典和模型。

注意:自定义类型中包含自定义类型,需要实现 + (NSDictionary *)mj_objectClassInArray 方法
@class Person;
@interface Group : NSObject
@property (nonatomic, strong) NSString *groupName;
@property (nonatomic, strong) NSArray<Person *> *members;
@property (nonatomic, strong) NSArray<Person *> *teachers;
@end

@implementation Group
+ (NSDictionary *)mj_objectClassInArray{
    return @{
             @"members": @"Person",
             @"teachers": [Person class]
             };
}
@end

2,YTKKeyValueStore 可以对系统类型的数据进行存储,对于自定义类型,将自定义类型处理成Json数据再进行存储即可。

#import <Foundation/Foundation.h>
#import "YTKKeyValueStore.h"

@interface YTKKeyValueStore (CustomObject)

/******** 自定义类型相关 *********************************************************/
/** 存储自定义类型 */
- (void)putCustomObject:(id)object
                 withId:(NSString *)objectId
              intoTable:(NSString *)tableName;

/** 存储包含自定义类型的数组 */
- (void)putArrWithCustomClass:(Class)aclass
                       object:(id)object
                       withId:(NSString *)objectId
                    intoTable:(NSString *)tableName;

/** 获取自定义类型的对象方法 */
- (id)getCustomClass:(Class)aclass
          objectById:(NSString *)objectId
           fromTable:(NSString *)tableName;

@end

@interface AGKeyValueManage : NSObject

+ (void)storeWithDBName:(NSString *)dbName tableName:(NSString *)tableName handle:(void (^)(YTKKeyValueStore *store, NSString *tableName))handle;

@end
#import "AGKeyValueManage.h"
#import "MJExtension.h"

@implementation YTKKeyValueStore (CustomObject)

- (void)putCustomObject:(id)object withId:(NSString *)objectId intoTable:(NSString *)tableName{
    NSDictionary *dic = [object mj_keyValues];
    [self putObject:dic withId:objectId intoTable:tableName];
}

/**
    将包含自定义对象的数组转成包含
 */
- (void)putArrWithCustomClass:(Class)aclass object:(id)object withId:(NSString *)objectId intoTable:(NSString *)tableName{
    NSArray *dicArr = [aclass mj_keyValuesArrayWithObjectArray:object];
    [self putObject:dicArr withId:objectId intoTable:tableName];
}

- (id)getCustomClass:(Class)aclass objectById:(NSString *)objectId fromTable:(NSString *)tableName{
    id object = [self getObjectById:objectId fromTable:tableName];
    id result;
    if ([object isKindOfClass:[NSArray class]]) {
        result = [aclass mj_objectArrayWithKeyValuesArray:object];
    }
    else{
        result = [aclass mj_objectWithKeyValues:object];
    }
    return result;
}
@end

@implementation AGKeyValueManage

+ (void)storeWithDBName:(NSString *)dbName tableName:(NSString *)tableName handle:(void (^)(YTKKeyValueStore *store, NSString *tableName))handle{
    if (!handle) {
        return;
    }
    YTKKeyValueStore *store = [[YTKKeyValueStore alloc] initDBWithName:dbName];
    [store createTableWithName:tableName];
    handle(store, tableName);
}
@end
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,276评论 4 61
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,206评论 19 139
  • 五代时期的后周国祚虽然短暂,但出现了两个雄才大略的皇帝,他们分别是周太祖郭威和周世宗柴荣,可惜这两位皇帝都英年早逝...
    硕鼠无止阅读 834评论 1 7
  • 《面包树上的女人》讲述的一个女人从少女时代到女人的爱情经历,起初爱上一位才子,才子答应她每年给她写一首歌,但因为才...
    cb01beb04684阅读 843评论 0 0
  • 禁锢 南方小城、淅淅沥沥的小雨打湿了所有心绪、周围嘈杂混乱、广场舞的音乐在沉郁的城市上空盘旋、像一场没有回响的控诉...
    猫不懂寂寞阅读 137评论 0 0