The Objective-C language defers as many decisions as it can from compile time and link time to runtime. Whenever possible, it does things dynamically. This means that the language requires not just a compiler, but also a runtime system to execute the compiled code. The runtime system acts as a kind of operating system for the Objective-C language; it’s what makes the language work.
This document looks at the NSObject class and how Objective-C programs interact with the runtime system. In particular, it examines the paradigms for dynamically loading new classes at runtime, and forwarding messages to other objects. It also provides information about how you can find information about objects while your program is running.
You should read this document to gain an understanding of how the Objective-C runtime system works and how you can take advantage of it. Typically, though, there should be little reason for you to need to know and understand this material to write a Cocoa application.
#pragma mark --- #pragma mark --- 获取某个实例的属性列表 --- + (NSMutableArray *)propertiesInfoWithInstance:(id)instance{ NSMutableArray *propertieAry = [NSMutableArray array]; unsigned int outCount, i; // 获取对象里的属性列表 objc_property_t * properties = class_copyPropertyList([instance class], &outCount); for (i = 0; i < outCount; i++) { objc_property_t property =properties[i]; // 属性名转成字符串 NSString *propertyName = [[NSString alloc] initWithCString:property_getName(property) encoding:NSUTF8StringEncoding]; [propertieAry addObject:propertyName]; } free(properties); return propertieAry; }
Runtime的使用02:获取某个类的成员变量列表
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#pragma mark --- #pragma mark --- 获取某个类的成员变量列表 --- + (NSMutableArray *)ivarInfoWithInstance:(id)instance{ NSMutableArray *ivarInfoAry = [NSMutableArray array]; unsigned int count, i; Ivar *ivars = class_copyIvarList([instance class], &count); for (i = 0 ; i < count ; i ++ ) {