iOS 使用coreData存贮页面的模型数据中的字典

我们使用coreData时候,会遇到较为复杂的数据类型的存贮,例如,我们要存一个模型,但是一个模型里面有个字典,这时候,我们该如何存贮呢

如图所示,一个对象中含有一个字典

我们实现一个公共的方法,将对象的所有属性都赋值给对应的coreData模型

复制代码
id makeRemoteModelToCoreDataModel(id remoteModel,Class remoteDataClass,NSManagedObject *coreDataModel) {
    int i;
    unsigned int propertyCount = 0;
    objc_property_t *propertyList = class_copyPropertyList(remoteDataClass, &propertyCount);
    
    
    NSMutableArray *propertyNameList = [NSMutableArray array];
    for ( i=0; i < propertyCount; i++ ) {
        objc_property_t *thisProperty = propertyList + i;
        const char* propertyName = property_getName(*thisProperty);
        NSString *string = [NSString stringWithFormat:@"%s",propertyName];
        [propertyNameList addObject:string];
    }
    
    if (propertyNameList.count > 0) {
        [propertyNameList enumerateObjectsUsingBlock:^(NSString* key, NSUInteger idx, BOOL *stop) {
            id obj = [remoteModel valueForKey:key];
            if (obj) {
                if ([obj isKindOfClass:[NSString class]]) {
                    [coreDataModel setValue:obj forKey:key];
                }else {
                    NSData *objData = [NSKeyedArchiver archivedDataWithRootObject:obj];
                    [coreDataModel setValue:objData forKey:key];
                }
            }
            
        }];
        free(propertyList);
        return coreDataModel;
    }else return nil;
}

存贮数据的时候调用

复制代码
            specialInfoDB = makeRemoteModelToCoreDataModel(specialBO, [specialObjectBO class],specialInfoDB);

在coreData的模型中添加对应的字段,只是数据类型是NSData

如图

读取数据的时候,将coredata的所有字段赋值给我们使用的模型对象

实现的方法

复制代码
id makeCoreDataModelToRemoteModel(id CoreDataModel,Class remoteDataClass) {
    int i;
    unsigned int propertyCount = 0;
    objc_property_t *propertyList = class_copyPropertyList(remoteDataClass, &propertyCount);
    
    
    NSMutableArray *propertyNameList = [NSMutableArray array];
    for ( i=0; i < propertyCount; i++ ) {
        objc_property_t *thisProperty = propertyList + i;
        const char* propertyName = property_getName(*thisProperty);
        NSString *string = [NSString stringWithFormat:@"%s",propertyName];
        [propertyNameList addObject:string];
    }
    
    if (propertyNameList.count > 0) {
        id dataModal = [[remoteDataClass alloc]init];
        [propertyNameList enumerateObjectsUsingBlock:^(NSString* key, NSUInteger idx, BOOL *stop) {
            id obj = [CoreDataModel valueForKey:key];
            if ([obj isKindOfClass:[NSString class]]) {
                [dataModal setValue:obj forKey:key];
            }else {
                id objFromData = [NSKeyedUnarchiver unarchiveObjectWithData:obj];
                [dataModal setValue:objFromData forKey:key];
            }
        }];
        free(propertyList);
        return dataModal;
    }else return nil;
}

调用

复制代码
        specialObjectBO* specialBO = makeCoreDataModelToRemoteModel(specialInfoDB, [specialObjectBO class]);

这两处重点用到了字典和NSData的相互转化

复制代码
NSData *dictData = [NSKeyedArchiver archivedDataWithRootObject:dict];

NSDictionary *subDict = [NSKeyedUnarchiver unarchiveObjectWithData:dictData];

注意点,我们CoreData中的字段是不能直接存贮我们自定义对象类型的,所以,如果我们要存贮的字段如果是一个自定义对象类型,则要将该字段成字段类型,然后转换成Data存贮

相关推荐
2501_9159214331 分钟前
iOS App 性能监控与优化实战 如何监控CPU、GPU、内存、帧率、耗电情况并提升用户体验(uni-app iOS开发调试必备指南)
android·ios·小程序·uni-app·iphone·webview·ux
开开心心loky1 小时前
[iOS] 属性关键字
macos·ios·objective-c·cocoa·xcode
2501_915921431 小时前
前端开发工具有哪些?常用前端开发工具、前端调试工具、前端构建工具与效率提升工具对比与最佳实践
android·前端·ios·小程序·uni-app·iphone·webview
2501_915918419 小时前
HTTPS 端口号详解 443 端口作用、iOS 抓包方法、常见 HTTPS 抓包工具与网络调试实践
android·网络·ios·小程序·https·uni-app·iphone
全栈技术负责人12 小时前
Hybrid应用性能优化实战分享(本文iOS 与 H5为例,安卓同理)
前端·ios·性能优化·html5
Zender Han13 小时前
Flutter 视频播放器——flick_video_player 介绍与使用
android·flutter·ios·音视频
咕噜签名分发冰淇淋15 小时前
苹果ios的系统app应用WebClip免签应用开源及方式原理
ios·开源·cocoa
2501_9151063218 小时前
App Store 软件上架全流程详解,iOS 应用发布步骤、uni-app 打包上传与审核要点完整指南
android·ios·小程序·https·uni-app·iphone·webview
开开心心loky18 小时前
[iOS] ViewController 的生命周期
macos·ui·ios·objective-c·cocoa
2501_916013741 天前
App 上架全流程指南,iOS App 上架步骤、App Store 应用发布流程、uni-app 打包上传与审核要点详解
android·ios·小程序·https·uni-app·iphone·webview