日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

用字典给Model赋值

發(fā)布時(shí)間:2025/3/15 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 用字典给Model赋值 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

用字典給Model賦值

此篇教程講述通過runtime擴(kuò)展NSObject,可以直接用字典給Model賦值,這是相當(dāng)有用的技術(shù)呢。

源碼:

NSObject+Properties.h 與?NSObject+Properties.m

// // NSObject+Properties.h // // Created by YouXianMing on 14-9-4. // Copyright (c) 2014年 YouXianMing. All rights reserved. //#import <Foundation/Foundation.h>@interface NSObject (Properties)- (void)setDataDictionary:(NSDictionary*)dataDictionary; - (NSDictionary *)dataDictionary;@end

// // NSObject+Properties.m // // Created by YouXianMing on 14-9-4. // Copyright (c) 2014年 YouXianMing. All rights reserved. //#import "NSObject+Properties.h" #import <objc/runtime.h>@implementation NSObject (Properties)#pragma public - 公開方法- (void)setDataDictionary:(NSDictionary*)dataDictionary {[self setAttributes:dataDictionary obj:self]; }- (NSDictionary *)dataDictionary {// 獲取屬性列表NSArray *properties = [self propertyNames:[self class]];// 根據(jù)屬性列表獲取屬性值return [self propertiesAndValuesDictionary:self properties:properties]; }#pragma private - 私有方法// 通過屬性名字拼湊setter方法 - (SEL)getSetterSelWithAttibuteName:(NSString*)attributeName {NSString *capital = [[attributeName substringToIndex:1] uppercaseString];NSString *setterSelStr = \[NSString stringWithFormat:@"set%@%@:", capital, [attributeName substringFromIndex:1]];return NSSelectorFromString(setterSelStr); }// 通過字典設(shè)置屬性值 - (void)setAttributes:(NSDictionary*)dataDic obj:(id)obj {// 獲取所有的key值NSEnumerator *keyEnum = [dataDic keyEnumerator];// 字典的key值(與Model的屬性值一一對(duì)應(yīng))id attributeName = nil;while ((attributeName = [keyEnum nextObject])){// 獲取拼湊的setter方法SEL sel = [obj getSetterSelWithAttibuteName:attributeName];// 驗(yàn)證setter方法是否能回應(yīng)if ([obj respondsToSelector:sel]){id value = nil;id tmpValue = dataDic[attributeName];if([tmpValue isKindOfClass:[NSNull class]]){// 如果是NSNull類型,則value值為空value = nil;}else{value = tmpValue;}// 執(zhí)行setter方法[obj performSelectorOnMainThread:selwithObject:valuewaitUntilDone:[NSThread isMainThread]];}} }// 獲取一個(gè)類的屬性名字列表 - (NSArray*)propertyNames:(Class)class {NSMutableArray *propertyNames = [[NSMutableArray alloc] init];unsigned int propertyCount = 0;objc_property_t *properties = class_copyPropertyList(class, &propertyCount);for (unsigned int i = 0; i < propertyCount; ++i){objc_property_t property = properties[i];const char *name = property_getName(property);[propertyNames addObject:[NSString stringWithUTF8String:name]];}free(properties);return propertyNames; }// 根據(jù)屬性數(shù)組獲取該屬性的值 - (NSDictionary*)propertiesAndValuesDictionary:(id)obj properties:(NSArray *)properties {NSMutableDictionary *propertiesValuesDic = [NSMutableDictionary dictionary];for (NSString *property in properties){SEL getSel = NSSelectorFromString(property);if ([obj respondsToSelector:getSel]){NSMethodSignature *signature = nil;signature = [obj methodSignatureForSelector:getSel];NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];[invocation setTarget:obj];[invocation setSelector:getSel];NSObject * __unsafe_unretained valueObj = nil;[invocation invoke];[invocation getReturnValue:&valueObj];//assign to @"" stringif (valueObj == nil){valueObj = @"";}propertiesValuesDic[property] = valueObj;}}return propertiesValuesDic; }@end

測(cè)試用Model

Model.h 與?Model.m

// // Model.h // Runtime // // Created by YouXianMing on 14-9-5. // Copyright (c) 2014年 YouXianMing. All rights reserved. //#import <Foundation/Foundation.h>@interface Model : NSObject@property (nonatomic, strong) NSString *name; @property (nonatomic, strong) NSNumber *age; @property (nonatomic, strong) NSDictionary *addressInfo; @property (nonatomic, strong) NSArray *events;@end

// // Model.m // Runtime // // Created by YouXianMing on 14-9-5. // Copyright (c) 2014年 YouXianMing. All rights reserved. //#import "Model.h"@implementation Model@end
使用時(shí)的情形
// // AppDelegate.m // Runtime // // Created by YouXianMing on 14-9-5. // Copyright (c) 2014年 YouXianMing. All rights reserved. //#import "AppDelegate.h" #import "NSObject+Properties.h" #import "Model.h"@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {// 初始化modelModel *model = [Model new];// 通過字典賦值model.dataDictionary = @{@"name" : @"YouXianMing",@"age" : @26,@"addressInfo" : @{@"HuBei": @"WeiHan"},@"events" : @[@"One", @"Two", @"Three"]};// 打印出屬性值NSLog(@"%@", model.dataDictionary);return YES; }@end

打印的信息:

2014-09-05 21:03:54.840 Runtime[553:60b] {

? ? addressInfo = ? ? {

? ? ? ? HuBei = WeiHan;

? ? };

? ? age = 26;

? ? events = ? ? (

? ? ? ? One,

? ? ? ? Two,

? ? ? ? Three

? ? );

? ? name = YouXianMing;

}

以下是兩段核心代碼(符合單一職能):

?

總結(jié)

以上是生活随笔為你收集整理的用字典给Model赋值的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。