一.实现runtime字典转模型
1.定义字典
2.定义模型属性
3.定义个长整型来接受模型的属性的个数 count
4.定义属性列表ivar
5.for循环遍历进行给模型属性赋值
6.转换成OC字符串
7. 截取字符串从第一个字符串 开始截取
8.进行KVC给模型赋值
//遍历属性 ,
//底层 runtime
NSDictionary *dict =
@{@"name" :
@"mingyuexin" ,
@"age" :
@18 ,
@"height" :
@1.8};
CZPerson *person = [[CZPerson
alloc]init];
//字典转模型
用
字典中的 key
对应的值
赋值给
跟这个key一样的
属性
//遍历
模型
哪些属性?
unsigned
int count =
0;
Ivar *ivar =
class_copyIvarList([CZPerson
class], &count);
//char a[10] = 'hello';
//数组名称是
常量指针
for (int
i = 0; i < count; ++i) {
Ivar v = ivar[i];
const
char *ivarName =
ivar_getName(v);
NSLog(@"%@",[NSString
stringWithCString:ivarName
encoding:NSUTF8StringEncoding]);
//传换成C字符串
NSString *str = [NSString
stringWithCString:ivarName
encoding:NSUTF8StringEncoding];
NSLog(@"%@",str);
//截取字符串
str = [str substringFromIndex:1];
//KVX模型赋值
[person setValue:dict[str]
forKey:str];
}
1. 交换方法实现
#import
"UIImage+XL.h"
#import <objc/runtime.h>
@implementation UIImage (XL)
//当前类被加载的时候
执行一次
+(void)load
{
//我们交换的就是这个两个方法
//imageWithName
//imageNamed
Method m1 =
class_getClassMethod([UIImage
class],
@selector(imageWithName:));
Method m2 =
class_getClassMethod([UIImage
class],
@selector(imageNamed:));
//交换方法
想当于你调用了自己方法其实调用的别人的.
method_exchangeImplementations(m1, m2);
}
+ (instancetype)imageWithName:(NSString
*)name
{
NSMutableString *str = [NSMutableString
stringWithString:name];
UIImage *newImage =
nil;
if ([[UIDevice
currentDevice].systemVersion
floatValue] >
8.0) {
[str appendString:@"_os8"];
newImage = [UIImage
imageWithName:str];
}else{
newImage = [UIImage
imageWithName:name];
}
if (newImage ==
nil) {
newImage = [UIImage
imageWithName:name];
}
return newImage;
}
3.实现数组越界了一样不会蹦,,交换方法实现,
#import
"NSMutableArray+XL.h"
#import <objc/runtime.h>
@implementation NSMutableArray (XL)
//当前类被加载的时候
执行一次
+(void)load
{
Method m1 =
class_getInstanceMethod(NSClassFromString(@"__NSArrayM"),
@selector(addObject:));
Method m2 =
class_getInstanceMethod(NSClassFromString(@"__NSArrayM"),
@selector(cz_addobjc:));
//交换方法实现
method_exchangeImplementations(m2, m1);
}
//执行方法
- (void)cz_addobjc:(id)objc
{
if (objc ==
nil) {
[self
cz_addobjc:@"你个傻X"];
}else{
[self
cz_addobjc:objc];
}
}
NSMutableArray *array = [NSMutableArray
array];
[array addObject:@"111"];
//添加一个空
对象
[array addObject:nil];
[array addObject:@"222"];
NSLog(@"%@",array);
//数组越界
// NSLog(@"%@",array[5]);
NSDictionary *dict = @{@"name" : @"mingyuexin" , @"age" : @18 , @"height" : @1.8};
CZPerson *person = [[CZPerson alloc]init];
//字典转模型 用 字典中的 key 对应的值 赋值给 跟这个key一样的 属性
//遍历 模型 哪些属性?
unsigned int count = 0;
Ivar *ivar = class_copyIvarList([CZPerson class], &count);
//char a[10] = 'hello';
//数组名称是 常量指针
for (int i = 0; i < count; ++i) {
Ivar v = ivar[i];
const char *ivarName = ivar_getName(v);
NSLog(@"%@",[NSString stringWithCString:ivarName encoding:NSUTF8StringEncoding]);
//传换成C字符串
NSString *str = [NSString stringWithCString:ivarName encoding:NSUTF8StringEncoding];
NSLog(@"%@",str);
//截取字符串
str = [str substringFromIndex:1];
//KVX模型赋值
[person setValue:dict[str] forKey:str];
#import <objc/runtime.h>
@implementation UIImage (XL)
//当前类被加载的时候 执行一次
+(void)load
Method m2 = class_getClassMethod([UIImage class], @selector(imageNamed:));
//交换方法 想当于你调用了自己方法其实调用的别人的.
method_exchangeImplementations(m1, m2);
{
NSMutableString *str = [NSMutableString stringWithString:name];
UIImage *newImage = nil;
if ([[UIDevice currentDevice].systemVersion floatValue] > 8.0) {
[str appendString:@"_os8"];
newImage = [UIImage imageWithName:str];
}else{
newImage = [UIImage imageWithName:name];
}
if (newImage == nil) {
newImage = [UIImage imageWithName:name];
#import <objc/runtime.h>
@implementation NSMutableArray (XL)
//当前类被加载的时候 执行一次
+(void)load
{
Method m1 = class_getInstanceMethod(NSClassFromString(@"__NSArrayM"), @selector(addObject:));
Method m2 = class_getInstanceMethod(NSClassFromString(@"__NSArrayM"), @selector(cz_addobjc:));
//交换方法实现
method_exchangeImplementations(m2, m1);
}
//执行方法
- (void)cz_addobjc:(id)objc
{
if (objc == nil) {
[self cz_addobjc:@"你个傻X"];
}else{
[self cz_addobjc:objc];
}
[array addObject:@"111"];
//添加一个空 对象
[array addObject:nil];
[array addObject:@"222"];
NSLog(@"%@",array);
//数组越界