运行时 #import <objc/runtime.h>
来源:互联网 发布:手机淘宝设置 店铺分类 编辑:程序博客网 时间:2024/04/27 19:46
一.实现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 = [[CZPersonalloc]init];
//字典转模型 用字典中的 key对应的值赋值给跟这个key一样的属性
//遍历模型哪些属性?
unsignedint count =0;
Ivar *ivar = class_copyIvarList([CZPersonclass], &count);
//char a[10] = 'hello';
//数组名称是常量指针
for (int i = 0; i < count; ++i) {
Ivar v = ivar[i];
constchar *ivarName = ivar_getName(v);
NSLog(@"%@",[NSStringstringWithCString:ivarNameencoding:NSUTF8StringEncoding]);
//传换成C字符串
NSString *str = [NSStringstringWithCString:ivarNameencoding: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([UIImageclass],@selector(imageWithName:));
Method m2 =class_getClassMethod([UIImageclass],@selector(imageNamed:));
//交换方法想当于你调用了自己方法其实调用的别人的.
method_exchangeImplementations(m1, m2);
}+ (instancetype)imageWithName:(NSString *)name
{
NSMutableString *str = [NSMutableStringstringWithString:name];
UIImage *newImage =nil;
if ([[UIDevicecurrentDevice].systemVersionfloatValue] >8.0) {
[str appendString:@"_os8"];
newImage = [UIImageimageWithName:str];
}else{
newImage = [UIImageimageWithName:name];
}
if (newImage ==nil) {
newImage = [UIImageimageWithName: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) {
[selfcz_addobjc:@"你个傻X"];
}else{
[selfcz_addobjc:objc];
}
}
NSMutableArray *array = [NSMutableArrayarray];
[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 = [[CZPersonalloc]init];
//字典转模型 用字典中的 key对应的值赋值给跟这个key一样的属性
//遍历模型哪些属性?
unsignedint count =0;
Ivar *ivar = class_copyIvarList([CZPersonclass], &count);
//char a[10] = 'hello';
//数组名称是常量指针
for (int i = 0; i < count; ++i) {
Ivar v = ivar[i];
constchar *ivarName = ivar_getName(v);
NSLog(@"%@",[NSStringstringWithCString:ivarNameencoding:NSUTF8StringEncoding]);
//传换成C字符串
NSString *str = [NSStringstringWithCString:ivarNameencoding: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([UIImageclass],@selector(imageNamed:));
//交换方法想当于你调用了自己方法其实调用的别人的.
method_exchangeImplementations(m1, m2);
{
NSMutableString *str = [NSMutableStringstringWithString:name];
UIImage *newImage =nil;
if ([[UIDevicecurrentDevice].systemVersionfloatValue] >8.0) {
[str appendString:@"_os8"];
newImage = [UIImageimageWithName:str];
}else{
newImage = [UIImageimageWithName:name];
}
if (newImage ==nil) {
newImage = [UIImageimageWithName: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) {
[selfcz_addobjc:@"你个傻X"];
}else{
[selfcz_addobjc:objc];
}
[array addObject:@"111"];
//添加一个空对象
[array addObject:nil];
[array addObject:@"222"];
NSLog(@"%@",array);
//数组越界
0 0
- 运行时 #import <objc/runtime.h>
- objc/runtime.h 相关
- objc/runtime.h 相关
- Swift/Objc的Runtime(运行时)机制
- IOS-usr/include-objc-runtime.h
- Objective-C中Runtime(objc/runtime.h)[细读]
- ObjC Runtime
- Objc Runtime
- Objc Runtime
- objc runtime
- objc/runtime.h之Associated Objects ==关联
- iOS在分类中添加属性,运行时编程,objc runtime 动态增加属性
- #import </usr/include/objc/objc-class.h> not such file or directory问题的解决方法
- #import </usr/include/objc/objc-class.h> not such file or directory问题的解决方法
- iOS 开发 Runtime.h / objc.h / message.h中的那些重要的声明
- Objc Runtime 学习笔记
- Objc-Runtime理解
- objc的runtime
- YumRepo Error: All mirror URLs are not using ftp, http[s] or file. Eg. </HTML>/ 标签: yum 2014-05-26
- [python]pygame游戏开发学习(三)
- RHEL6 64bit下更改YUM配置。yum this system is not registered with rhn的解决办法
- YUM更换源(1)--yum找不到安装包 2013-01-18 20:08 8687人阅读 评论(1) 收藏 举报 分类: linux(70) 公司提供的CentOS VM中,/etc/yum.r
- 文章标题
- 运行时 #import <objc/runtime.h>
- C++之设计模式列表简括
- 机器学习笔记_ 最大熵模型
- 博客积分规则
- C++之定义模板的demo
- Effective C++ 笔记 第八部分 定制new和delete
- [nodejs入门]第一篇,javascript基础(1)
- Shell之lsof命令简介
- 坐标系统 Qt 学习之路 2(28):坐标系统