你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS NSDictionary和Model

iOS NSDictionary和Model

編輯:IOS開發綜合

此篇教程講述通過runtime擴展NSObject,可以直接用字典給Model賦值,這是相當有用的技術呢。

 

 

 

關於runtime的詳情以後會詳細介紹

源碼:

NSObject+Property.h 與 NSObject+Property.m

//
//  NSObject+Property.h
//  ModelValueTest1.0
//
//  Created by Lisa on 14-9-15.
//  Copyright (c) 2014年 Lisa. All rights reserved.
//
#import 
@interface NSObject (Property)
-(void)setDataDictionary:(NSDictionary *)dataDictionary;
-(NSDictionary*)dataDictionary;
@end

 

//
//  NSObject+Property.m
//  ModelValueTest1.0
//
//  Created by Lisa on 14-9-15.
//  Copyright (c) 2014年 Lisa. All rights reserved.
//
#import NSObject+Property.h
#import 
@implementation NSObject (Property)
#pragma mark--Public method
-(void)setDataDictionary:(NSDictionary *)dataDictionary
{
[self setAttributes:dataDictionary obj:self];
}
-(NSDictionary*)dataDictionary
{
//獲取屬性列表
NSArray *properties = [self propertyNames:[self class]];
//根據屬性列表獲取屬性值
return [self propertiesAndValuesDictionary:self properties:properties];
}
#pragma mark--- Private method
//通過屬性名字拼湊 setter 方法
-(SEL)getSetterSelWithAttibuteName:(NSString *)attributeName
{
NSString *captial = [[attributeName substringToIndex:1]uppercaseString];
NSString *setterSelStr = [NSString stringWithFormat:@set%@%@:,captial,[attributeName substringFromIndex:1]];
return NSSelectorFromString(setterSelStr);
}
//通過字典設置屬性
-(void)setAttributes:(NSDictionary *)dataDic obj:(id)obj
{
//獲取所有的key值
NSEnumerator *keyEnum = [dataDic keyEnumerator];
//字典的key值(與model的屬性值一一對應)
id attrbuteName = nil;
while (attrbuteName = [keyEnum nextObject]) {
//獲取 拼湊的setter方法
SEL sel = [obj getSetterSelWithAttibuteName:attrbuteName];
//驗證setter方法是否能回應
if ([obj respondsToSelector:sel]) {
id value = nil;
id tmpValue = dataDic[attrbuteName];
if ([tmpValue isKindOfClass:[NSNull class]]) {
//如果是NSNull 類型,則 value值為空
value = nil;
}
else
{
value = tmpValue;
}
//執行setter 方法
[obj performSelectorOnMainThread:sel withObject:value waitUntilDone:[NSThread isMainThread]];
}
}
}
//獲取一個類的屬性名字列表
-(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

測試的model

LModelItems.h與LModelItems.m

//
//  LModelItems.h
//  ModelValueTest1.0
//
//  Created by Lisa on 14-9-15.
//  Copyright (c) 2014年 Lisa. All rights reserved.
//
#import 
@interface LModelItems : NSObject
@property(nonatomic,strong)NSString *name;
@property(nonatomic,strong)NSNumber *age;
@property(nonatomic,strong)NSDictionary *addressDic;
@property(nonatomic,strong)NSArray *eventd;
@end

 

//
//  LModelItems.m
//  ModelValueTest1.0
//
//  Created by Lisa on 14-9-15.
//  Copyright (c) 2014年 Lisa. All rights reserved.
//
#import LModelItems.h
@implementation LModelItems
@end

使用的場景:

在控制器中 >>>>>LViewController.h與LViewController.m

//  LViewController.h
//  ModelValueTest1.0
//
//  Created by Lisa on 14-9-15.
//  Copyright (c) 2014年 Lisa. All rights reserved.
//
#import 
@interface LViewController : UIViewController
@end

 

//
//  LViewController.m
//  ModelValueTest1.0
//
//  Created by itotem on 14-9-15.
//  Copyright (c) 2014年 Lisa. All rights reserved.
//
#import LViewController.h
#import NSObject+Property.h
#import LModelItems.h
@interface LViewController ()
@end
@implementation LViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
LModelItems *model = [LModelItems new];
//通過字典賦值
model.dataDictionary = @{@name:@Lisa,@age:@26,@addressDic:@{@provinces:@北京,@area:@海澱區},@eventd:@[@first,@second,@third]};
//打印賦值結果
NSLog(@%@,model.dataDictionary);
NSLog(@name=%@,model.name);
NSLog(@age=%@,model.age);
NSLog(@addressDic=%@,[model.addressDic objectForKey:@provinces]);
NSLog(@eventd=%@,model.eventd);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end

打印信息:

2014-09-15 17:11:36.948 ModelValueTest1.0[8386:60b] {

addressDic = {

area = U6d77U6dc0U533a;

provinces = U5317U4eac;

};

age = 26;

eventd = (

first,

second,

third

);

name = Lisa;

}

2014-09-15 17:11:36.950 ModelValueTest1.0[8386:60b] name=Lisa

2014-09-15 17:11:36.951 ModelValueTest1.0[8386:60b] age=26

2014-09-15 17:11:36.952 ModelValueTest1.0[8386:60b] addressDic=北京

2014-09-15 17:11:36.952 ModelValueTest1.0[8386:60b] eventd=(

first,

second,

third

)

 

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

height=448

height=515

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved