runtime - 运行时机制1
来源:互联网 发布:剑3少林捏脸数据 编辑:程序博客网 时间:2024/05/17 03:32
unsignedint outCount;
int i;
objc_property_t *pProperty = class_copyPropertyList([UIDatePicker class], &outCount);
for (i = outCount -1; i >= 0; i--)
{
// 循环获取属性的名字
property_getName函数返回一个属性的名称
NSString *getPropertyName = [NSString stringWithCString:property_getName(pProperty[i]) encoding:NSUTF8StringEncoding];
NSString *getPropertyNameString = [NSString stringWithCString:property_getAttributes(pProperty[i]) encoding:NSUTF8StringEncoding];
if([getPropertyName isEqualToString:@"textColor"])
{
[picker setValue:[UIColor whiteColor] forKey:@"textColor"];
}
NSLog(@"%@====%@",getPropertyNameString,getPropertyName);
}
runtime : 运行时机制
1.是什么
1> runtime是一套比较底层的纯C语言API, 属于1个C语言库, 包含了很多底层的C语言API
2> 平时编写的OC代码, 在程序运行过程中, 其实最终都是转成了runtime的C语言代码, runtime算是OC的幕后工作者
3> 举例:
OC :
[[MJPerson alloc] init]
runtime :消息机制
objc_msgSend(objc_msgSend("MJPerson" , "alloc"), "init")
2.用过么? 怎么用?
1> runtime是属于OC的底层, 可以进行一些非常底层的操作(用OC是无法现实的, 不好实现)
* 在程序运行过程中, 动态创建一个类(比如KVO的底层实现,KVC的底层)实际上整个OC都会用到runtime
* 在程序运行过程中, 动态地为某个类添加属性\方法, 修改属性值\方法
* 遍历一个类的所有成员变量(属性)\所有方法
3.相关的头文件和函数
1> 头文件
* <objc/runtime.h>
* <objc/message.h>
2> 相关应用
* NSCoding(归档和解档, 利用runtime遍历模型对象的所有属性)
* 字典--> 模型 (利用runtime遍历模型对象的所有属性, 根据属性名从字典中取出对应的值, 设置到模型的属性上)
* KVO(利用runtime动态产生一个类)
* 用于封装框架(想怎么改就怎么改)
3> 相关函数
* objc_msgSend : 给对象发送消息
* class_copyMethodList : 遍历某个类所有的方法
* class_copyIvarList : 遍历某个类所有的成员变量
* class_.....
4.必备常识
1> Ivar : 成员变量
2> Method : 成员方法
#import "MJViewController.h"
#import "MJPerson.h"
#import "MJDog.h"
#import <objc/runtime.h>
#import <objc/message.h>
@interface MJViewController ()
@property (nonatomic, strong) MJPerson *person;
@property (nonatomic, strong) MJDog *dog;
@end
@implementation MJViewController
- (void)viewDidLoad
{
[super viewDidLoad];
unsigned int count = 0;
Ivar *ivars = class_copyIvarList([MJPerson class], &count);
NSLog(@"%u", count);
for (int i = 0; i<count; i++) {
// 取出i位置对应的成员变量
Ivar ivar = ivars[i];
// 查看成员变量
const char *name = ivar_getName(ivar);
NSLog(@"%s", name);
}
self.person = [[MJPerson alloc] init];
self.dog = [[MJDog alloc] init];
// NSLog(@"%@ %@", self.person.class, self.person)
// 让self.dog监听self.person的age属性的改变,当人的属性改变了,调用狗的方法
[self.person addObserver:self.dog forKeyPath:@"age" options:0 context:nil];
// Foundation
// NSDictionary
// NSArray
// NSString
// NSArray *array = [NSArray array];
// array.count;
// Core Foundation
// CFDictionaryRef
// CFArrayRef
// CFStringRef
// CFArrayRef array2 = CFArrayCreate(NULL, NULL, 10, NULL);
// CFArrayGetCount(array2);
NSString *str = @"dfshgfkhgsdkhgf";
// (__bridge <#type#>)<#expression#>
// 在Foundation和Core Foundation数据之间进行转换(没有做任何的内存管理, 只是简单的转换)
// NSArray *array1 = [[NSArray alloc] init];
//
// CFArrayRef array2 = (__bridge_retained CFArrayRef)array1;
//
// [array1 release];
// (__bridge_retained <#CF type#>)<#expression#>
// Foundation --> Core Foundation
// (__bridge_transfer <#Objective-C type#>)<#expression#>
// Core Foundation --> Foundation
// CFArrayRef array3 = CFArrayCreate(NULL, NULL, 10, NULL);
//
// NSArray *array4 = (__bridge_transfer NSArray *)array3;
//
// CFRelease(array3);
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
self.person.age = 30;
}
@end
#import <Foundation/Foundation.h>
@interface MJPerson : NSObject <NSCoding>
@property (nonatomic, assign) int age;
@property (nonatomic, assign) int height;
@property (nonatomic, copy) NSString *name;
//@property (nonatomic, assign) int age3;
//@property (nonatomic, assign) int age4;
//@property (nonatomic, assign) int age5;
//@property (nonatomic, assign) int age6;
@end
// MJPerson.m
#import "MJPerson.h"
#import <objc/runtime.h>
@implementation MJPerson
// 归档
- (void)encodeWithCoder:(NSCoder *)encoder
{
unsigned int count = 0;
Ivar *ivars = class_copyIvarList([MJPerson class], &count);
for (int i = 0; i<count; i++) {
// 取出i位置对应的成员变量
Ivar ivar = ivars[i];
// 查看成员变量
const char *name = ivar_getName(ivar);
// 归档
NSString *key = [NSString stringWithUTF8String:name];
id value = [self valueForKey:key];
[encoder encodeObject:value forKey:key];
}
free(ivars);
}
// 解档
- (id)initWithCoder:(NSCoder *)decoder
{
if (self = [super init]) {
unsigned int count = 0;
Ivar *ivars = class_copyIvarList([MJPerson class], &count);
for (int i = 0; i<count; i++) {
// 取出i位置对应的成员变量
Ivar ivar = ivars[i];
// 查看成员变量
const char *name = ivar_getName(ivar);
// 归档
NSString *key = [NSString stringWithUTF8String:name];
id value = [decoder decodeObjectForKey:key];
// 设置到成员变量身上
[self setValue:value forKey:key];
}
free(ivars);
}
return self;
}
@end
0 0
- runtime - 运行时机制1
- runtime 运行时机制
- runtime 运行时机制
- Runtime运行时机制
- 运行时机制runtime
- runtime 运行时机制
- runtime-运行时机制
- Runtime运行时机制
- runtime 运行时机制
- runtime 运行时机制
- runtime 运行时机制
- runtime 运行时机制
- Runtime运行时机制
- Runtime运行时机制
- runtime运行时机制
- Runtime运行时机制
- runtime : 运行时机制
- runtime 运行时机制
- JMeter数据库请求配置及示例
- 左右滑动的Tab
- maven 简介
- CDN
- hdu 2089——不要62
- runtime - 运行时机制1
- Java面向对象编程
- 【SPOJ-COINS】Bytelandian gold coins【DP】
- Android的快速开发框架 afinal
- 【SQL Server】Session blocking
- zstu 2545 地道战
- JAVA_继承演示04_FoodManagerDeamo.java____super关键字____冒泡排序
- Android studio 修改项目列表的字体大小
- web.xml 中的listener、 filter、servlet 加载顺序及其详解