文档:Objective-C Runtime Programming Guide
大概意思(有时间详细整理该章节):
Objectvie-c Runtime
The runtime system acts as a kind of opeatim system for the Objective-C language.
1.Runtime Versions and Platforms
1.1分两个版本,OC和OC-2,新版本的runtime有新特性
1.2手机应用用的是最新版本的runtime, OS 10.5及其以上也是,其他的都是老版本的runtime
2.Interacting with the Runtime
OC跟 runtime system交互,有三个levels:Objectiive-c Source Code,NSOject Methods,Runtim Functions:
Objective -C Source Code: 编译阶段
NSObject Methods: 主要是NSObject的一些方法调用,如:isKindOfClass,isMemberOfClass,respondsToSelector,conformsToProtocol
Runtime Functions:
/usr/include/objc 里面的一些方法调用
3.Messaging
主要说 objc_msgSend 函数的调用。
Type Encodings
Declared Properties
可以获取到类里面的属性方法名
Property Type and Functions
an opaque handle to a property descriptor:
typedef struct objc_property *Property;
用下面两个方法可以获取到类的全部属性和全部协议
objc_property_t *class_copyPropertyList(Class cls, unsigned int *outCount)
objc_property_t *protocol_copyPropertyList(Protocol *proto, unsigned int *outCount)
如:
- @interface Lender : NSObject {
- float alone;
- }
- @property float alone;
- @end
获取属性列表:
- id LenderClass = objc_getClass("Lender");
- unsigned int outCount;
- objc_property_t *properties = class_copyPropertyList(LenderClass, &outCount);
得到属性名字:
- const charchar *property_getName(objc_property_t property)
- objc_property_t class_getProperty(Class cls, const charchar *name)
- objc_property_t protocol_getProperty(Protocol *proto, const charchar *name, BOOL isRequiredProperty, BOOL isInstanceProperty)
得到 @encode type的属性类型
const char *property_getAttributes(objc_property_t property)
例子:
- id LenderClass = objc_getClass("Lender");
- unsigned int outCount, i;
- objc_property_t *properties = class_copyPropertyList(LenderClass, &outCount);
- for (i = 0; i < outCount; i++) {
- objc_property_t property = properties[i];
- fprintf(stdout, "%s %s\n", property_getName(property), property_getAttributes(property));
- }
得到Property Type String
如:
- @property(nonatomic,weak)IBOutlet MKMapView *mapview;
- id LenderClass = objc_getClass("ViewController");
- unsigned int outCount, i;
- objc_property_t *properties = class_copyPropertyList(LenderClass, &outCount);
- for (i = 0; i < outCount; i++) {
- objc_property_t property = properties[i];
- fprintf(stdout, "%s %s\n", property_getName(property), property_getAttributes(property));
- }
打印的结果是:
mapview T@"MKMapView",W,N,V_mapview
即,开始是属性名字,后面以 T开始,V结束,中间是这个property的各种属性,如nonatomic等,后面会紧跟属性的实例名