美文网首页
【开发工具】YTKKeyValueStore 键值存储--存储自

【开发工具】YTKKeyValueStore 键值存储--存储自

作者: 小子爱搞事 | 来源:发表于2017-02-24 13:09 被阅读331次

    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
    

    相关文章

      网友评论

          本文标题:【开发工具】YTKKeyValueStore 键值存储--存储自

          本文链接:https://www.haomeiwen.com/subject/wehwwttx.html