run Time
来源:互联网 发布:国家中级程序员证书 编辑:程序博客网 时间:2024/05/16 15:18
Objective-C
Objective-C Objective-CObjc Runtime Objc RuntimeRuntime CC
Runtime
- C Cruntime
- [object doSomething] (object) (doSomething)runtime
Objective-C runtime Modern runtimeLegacy runtime Modern Runtime 64Mac OS X Apps iOS AppsLegacy Runtime 32 Mac OS X Apps
runtime Runtime
Class
Objective-C Classobjc_class typedef struct objc_class *Class;
objc/runtime.h objc_classstruct objc_class {
Search
Navigate... Navigate...
Navigate...
Class isa OBJC_ISA_AVAILABILITY;
#if !__OBJC2__
Class super_class
const char *name
long version
long info
long instance_size
struct objc_ivar_list *ivars
struct objc_method_list **methodLists
struct objc_cache *cache
struct objc_protocol_list *protocols
#endif
} OBJC2_UNAVAILABLE;
OBJC2_UNAVAILABLE; //
OBJC2_UNAVAILABLE; //
OBJC2_UNAVAILABLE; // 0 OBJC2_UNAVAILABLE; // OBJC2_UNAVAILABLE; // OBJC2_UNAVAILABLE; // OBJC2_UNAVAILABLE; // OBJC2_UNAVAILABLE; //
OBJC2_UNAVAILABLE; //
- isa Objective-CClass isametaClass( )
- super_class (NSObject NSProxy)super_class NULL
- cache isa
methodLists cachecache runtimecache cachemethodLists
4. version cache
NSArray *array = [[NSArray alloc] init];
- [NSArray alloc] NSArray+alloc NSObject
- NSObject +allocNSArray isaNSArray
+alloc cache - -init NSArraycache
- [[NSArray alloc] init] cache
objc_object id objc_object(objc/objc.h)
struct objc_object {
Class isa OBJC_ISA_AVAILABILITY;
};
typedef struct objc_object *id;
isa Objective-Cisa Runtimeselector
objc_object NSObjectalloc allocWithZone:class_createInstance objc_object
id objc_objectC++ Cvoid *
objc_cache
objc_class cacheobjc_cache
struct objc_cache {
unsigned int mask /* total = mask + 1 */
unsigned int occupied
Method buckets[1]
};
OBJC2_UNAVAILABLE;
OBJC2_UNAVAILABLE;
OBJC2_UNAVAILABLE;
- mask bucketObjective-C runtime selectorAND (index = (mask & selector))hash
- occupied bucket
- buckets Methodmask+1 NULLbucket
bucket
(Meta Class)
( )NSArray *array = [NSArray array];
+array NSArrayNSArray objc_objectisa isa+array isaobjc_class meta-class
meta-class runtimemeta-class
meta-class meta-class
meta-class isaObjective-C meta-classisa meta-classNSObject meta-classNSObject meta-classmeta-class isa
objc_class super_classmeta-class
NSObject meta-classmeta-class
void TestMetaClass(id self, SEL _cmd) {
NSLog(@"This objcet is %p", self);
NSLog(@"Class is %@, super class is %@", [self class], [self superclass]);
Class currentClass = [self class];
for (int i = 0; i < 4; i++) {
NSLog(@"Following the isa pointer %d times gives %p", i, currentClass);
currentClass = objc_getClass((__bridge void *)currentClass);
}
NSLog(@"NSObject's class is %p", [NSObject class]);
NSLog(@"NSObject's meta class is %p", objc_getClass((__bridge void *)[NSObject class]));
}
#pragma mark -
@implementation Test
- (void)ex_registerClassPair {
Class newClass = objc_allocateClassPair([NSError class], "TestClass", 0);
class_addMethod(newClass, @selector(testMetaClass), (IMP)TestMetaClass, "v@:");
objc_registerClassPair(newClass);
id instance = [[newClass alloc] initWithDomain:@"some domain" code:0 userInfo:nil];
[instance performSelector:@selector(testMetaClass)];
}
@end
NSError TestClasstestMetaClass TestMetaClass
2014-10-20 22:57:07.352 mountain[1303:41490] This objcet is 0x7a6e22b0
2014-10-20 22:57:07.353 mountain[1303:41490] Class is TestStringClass, super class is NSError
2014-10-20 22:57:07.353 mountain[1303:41490] Following the isa pointer 0 times gives 0x7a6e21b0
2014-10-20 22:57:07.353 mountain[1303:41490] Following the isa pointer 1 times gives 0x0
2014-10-20 22:57:07.353 mountain[1303:41490] Following the isa pointer 2 times gives 0x0
2014-10-20 22:57:07.353 mountain[1303:41490] Following the isa pointer 3 times gives 0x0
2014-10-20 22:57:07.353 mountain[1303:41490] NSObject's class is 0xe10000
2014-10-20 22:57:07.354 mountain[1303:41490] NSObject's meta class is 0x0
for objc_getClassisa NSObjectmeta-class 0x0NSObject meta-class
class meta-class
runtime classobjc object_
objc_class runtime
(name)
//
const char * class_getName ( Class cls );
● class_getName cls Nil(super_class)(meta-class)
//
Class class_getSuperclass ( Class cls );
// Class
BOOL class_isMetaClass ( Class cls );
● class_getSuperclass cls Nilcls NilNSObject superclass● class_isMetaClasscls YEScls NilNO
(instance_size)
//
size_t class_getInstanceSize ( Class cls );
(ivars) objc_classivars ivarsIvar( )runtime
1.
//
Ivar class_getInstanceVariable ( Class cls, const char *name );
//
Ivar class_getClassVariable ( Class cls, const char *name );
//
BOOL class_addIvar ( Class cls, const char *name, size_t size, uint8_t alignment, const char *types );
//
Ivar * class_copyIvarList ( Class cls, unsigned int *outCount );
● class_getInstanceVariablename objc_ivar(Ivar) ● class_getClassVariable Objective-CObjective-C
● Objective-C class_addIvarobjc_allocateClassPair objc_registerClassPair1<<alignment ivarlog2(sizeof(pointer_type))
● class_copyIvarList objc_ivar outCountfree()
2. //
objc_property_t class_getProperty ( Class cls, const char *name );
//
objc_property_t * class_copyPropertyList ( Class cls, unsigned int *outCount );
//
BOOL class_addProperty ( Class cls, const char *name, const objc_property_attribute_t *attributes, unsigned int attributeCount );
//
void class_replaceProperty ( Class cls, const char *name, const objc_property_attribute_t *attributes, unsigned int attributeCount );
ivars
3. MAC OS Xruntime strong/weak
const uint8_t * class_getIvarLayout ( Class cls );
void class_setIvarLayout ( Class cls, const uint8_t *layout );
const uint8_t * class_getWeakIvarLayout ( Class cls );
void class_setWeakIvarLayout ( Class cls, const uint8_t *layout );
objc_registerClassPair (methodLists)
//
BOOL class_addMethod ( Class cls, SEL name, IMP imp, const char *types );
//
Method class_getInstanceMethod ( Class cls, SEL name );
//
Method class_getClassMethod ( Class cls, SEL name );
//
Method * class_copyMethodList ( Class cls, unsigned int *outCount );
//
IMP class_replaceMethod ( Class cls, SEL name, IMP imp, const char *types );
//
IMP class_getMethodImplementation ( Class cls, SEL name );
IMP class_getMethodImplementation_stret ( Class cls, SEL name );
// selector
BOOL class_respondsToSelector ( Class cls, SEL sel );
● class_addMethod NO method_setImplementationObjective-C C—self _cmd(IMP )
void myMethodIMP(id self, SEL _cmd)
{
// implementation ....
}
types
● class_getInstanceMethodclass_getClassMethod class_copyMethodList
● class_copyMethodListclass_copyMethodList(object_getClass(cls), &count)() outCountfree()
● class_replaceMethod name class_addMethodname method_setImplementation
● class_getMethodImplementationmethod_getImplementation(class_getInstanceMethod(cls, name))runtime selector
● class_respondsToSelectorNSObject respondsToSelector:instancesRespondToSelector: (objc_protocol_list)
//
BOOL class_addProtocol ( Class cls, Protocol *protocol );
//
BOOL class_conformsToProtocol ( Class cls, Protocol *protocol );
//
Protocol * class_copyProtocolList ( Class cls, unsigned int *outCount );
● class_conformsToProtocolNSObject conformsToProtocol:● class_copyProtocolListfree()
(version)
//
int class_getVersion ( Class cls );
//
void class_setVersion ( Class cls, int version );
runtime CoreFoundationtool-free bridging Class objc_getFutureClass ( const char *name );
void objc_setFutureClass ( Class cls, const char *name );
(Example)
//-----------------------------------------------------------
// MyClass.h
@interface MyClass : NSObject <NSCopying, NSCoding>
@property (nonatomic, strong) NSArray *array;
@property (nonatomic, copy) NSString *string;
- (void)method1;
- (void)method2;
+ (void)classMethod1;
@end
//-----------------------------------------------------------
// MyClass.m
#import "MyClass.h"
@interface MyClass () {
NSInteger _instance1;
NSString * _instance2;
}
@property (nonatomic, assign) NSUInteger integer;
- (void)method3WithArg1:(NSInteger)arg1 arg2:(NSString *)arg2;
@end
@implementation MyClass
+ (void)classMethod1 {
}
- (void)method1 {
NSLog(@"call method method1");
}
- (void)method2 {
}
- (void)method3WithArg1:(NSInteger)arg1 arg2:(NSString *)arg2 {
NSLog(@"arg1 : %ld, arg2 : %@", arg1, arg2);
}
@end
//-----------------------------------------------------------
// main.h
#import "MyClass.h"
#import "MySubClass.h"
#import <objc/runtime.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
MyClass *myClass = [[MyClass alloc] init];
unsigned int outCount = 0;
Class cls = myClass.class;
//
NSLog(@"class name: %s", class_getName(cls));
NSLog(@"==========================================================");
//
NSLog(@"super class name: %s", class_getName(class_getSuperclass(cls))); NSLog(@"==========================================================");
//
NSLog(@"MyClass is %@ a meta-class", (class_isMetaClass(cls) ? @"" : @"not")); NSLog(@"==========================================================");
Class meta_class = objc_getMetaClass(class_getName(cls));
NSLog(@"%s's meta-class is %s", class_getName(cls), class_getName(meta_class));
NSLog(@"==========================================================");
//
NSLog(@"instance size: %zu", class_getInstanceSize(cls)); NSLog(@"==========================================================");
//
Ivar *ivars = class_copyIvarList(cls, &outCount); for (int i = 0; i < outCount; i++) {
Ivar ivar = ivars[i];
NSLog(@"instance variable's name: %s at index: %d", ivar_getName(ivar), i);
}
free(ivars);
Ivar string = class_getInstanceVariable(cls, "_string");
if (string != NULL) {
NSLog(@"instace variable %s", ivar_getName(string));
}
NSLog(@"==========================================================");
//
objc_property_t * properties = class_copyPropertyList(cls, &outCount); for (int i = 0; i < outCount; i++) {
objc_property_t property = properties[i];
NSLog(@"property's name: %s", property_getName(property));
}
free(properties);
objc_property_t array = class_getProperty(cls, "array");
if (array != NULL) {
NSLog(@"property %s", property_getName(array));
}
NSLog(@"==========================================================");
//
Method *methods = class_copyMethodList(cls, &outCount); for (int i = 0; i < outCount; i++) {
Method method = methods[i];
NSLog(@"method's signature: %s", method_getName(method));
}
free(methods);
Method method1 = class_getInstanceMethod(cls, @selector(method1));
if (method1 != NULL) {
NSLog(@"method %s", method_getName(method1));
}
Method classMethod = class_getClassMethod(cls, @selector(classMethod1));
if (classMethod != NULL) {
NSLog(@"class method : %s", method_getName(classMethod));
}
NSLog(@"MyClass is%@ responsd to selector: method3WithArg1:arg2:", class_respondsToSelector(cls, @selector(method3WithArg1:arg2:)) ? @
IMP imp = class_getMethodImplementation(cls, @selector(method1));
imp();
NSLog(@"==========================================================");
//
Protocol * __unsafe_unretained * protocols = class_copyProtocolList(cls, &outCount); Protocol * protocol;
for (int i = 0; i < outCount; i++) {
protocol = protocols[i];
NSLog(@"protocol name: %s", protocol_getName(protocol));
}
NSLog(@"MyClass is%@ responsed to protocol %s", class_conformsToProtocol(cls, protocol) ? @"" : @" not", protocol_getName(protocol));
NSLog(@"==========================================================");
}
return 0; }
2014-10-22 19:41:37.452 RuntimeTest[3189:156810] class name: MyClass
2014-10-22 19:41:37.453 RuntimeTest[3189:156810] ==========================================================
2014-10-22 19:41:37.454 RuntimeTest[3189:156810] super class name: NSObject
2014-10-22 19:41:37.454 RuntimeTest[3189:156810] ==========================================================
2014-10-22 19:41:37.454 RuntimeTest[3189:156810] MyClass is not a meta-class
2014-10-22 19:41:37.454 RuntimeTest[3189:156810] ==========================================================
2014-10-22 19:41:37.454 RuntimeTest[3189:156810] MyClass's meta-class is MyClass
2014-10-22 19:41:37.455 RuntimeTest[3189:156810] ==========================================================
2014-10-22 19:41:37.455 RuntimeTest[3189:156810] instance size: 48
2014-10-22 19:41:37.455 RuntimeTest[3189:156810] ==========================================================
2014-10-22 19:41:37.455 RuntimeTest[3189:156810] instance variable's name: _instance1 at index: 0
2014-10-22 19:41:37.455 RuntimeTest[3189:156810] instance variable's name: _instance2 at index: 1
2014-10-22 19:41:37.455 RuntimeTest[3189:156810] instance variable's name: _array at index: 2
2014-10-22 19:41:37.455 RuntimeTest[3189:156810] instance variable's name: _string at index: 3
2014-10-22 19:41:37.463 RuntimeTest[3189:156810] instance variable's name: _integer at index: 4
2014-10-22 19:41:37.463 RuntimeTest[3189:156810] instace variable _string
2014-10-22 19:41:37.463 RuntimeTest[3189:156810] ==========================================================
2014-10-22 19:41:37.463 RuntimeTest[3189:156810] property's name: array
2014-10-22 19:41:37.463 RuntimeTest[3189:156810] property's name: string
2014-10-22 19:41:37.464 RuntimeTest[3189:156810] property's name: integer
2014-10-22 19:41:37.464 RuntimeTest[3189:156810] property array
2014-10-22 19:41:37.464 RuntimeTest[3189:156810] ==========================================================
2014-10-22 19:41:37.464 RuntimeTest[3189:156810] method's signature: method1
2014-10-22 19:41:37.464 RuntimeTest[3189:156810] method's signature: method2
2014-10-22 19:41:37.464 RuntimeTest[3189:156810] method's signature: method3WithArg1:arg2:
2014-10-22 19:41:37.465 RuntimeTest[3189:156810] method's signature: integer
2014-10-22 19:41:37.465 RuntimeTest[3189:156810] method's signature: setInteger:
"
2014-10-22 19:41:37.465 RuntimeTest[3189:156810] method's signature: array
2014-10-22 19:41:37.465 RuntimeTest[3189:156810] method's signature: string
2014-10-22 19:41:37.465 RuntimeTest[3189:156810] method's signature: setString:
2014-10-22 19:41:37.465 RuntimeTest[3189:156810] method's signature: setArray:
2014-10-22 19:41:37.466 RuntimeTest[3189:156810] method's signature: .cxx_destruct
2014-10-22 19:41:37.466 RuntimeTest[3189:156810] method method1
2014-10-22 19:41:37.466 RuntimeTest[3189:156810] class method : classMethod1
2014-10-22 19:41:37.466 RuntimeTest[3189:156810] MyClass is responsd to selector: method3WithArg1:arg2:
2014-10-22 19:41:37.467 RuntimeTest[3189:156810] call method method1
2014-10-22 19:41:37.467 RuntimeTest[3189:156810] ==========================================================
2014-10-22 19:41:37.467 RuntimeTest[3189:156810] protocol name: NSCopying
2014-10-22 19:41:37.467 RuntimeTest[3189:156810] protocol name: NSCoding
2014-10-22 19:41:37.467 RuntimeTest[3189:156810] MyClass is responsed to protocol NSCoding
2014-10-22 19:41:37.468 RuntimeTest[3189:156810] ==========================================================
runtime
//
Class objc_allocateClassPair ( Class superclass, const char *name, size_t extraBytes );
//
void objc_disposeClassPair ( Class cls );
// objc_allocateClassPair void objc_registerClassPair ( Class cls );
● objc_allocateClassPairsuperclass NilextraBytes 0ivars
objc_allocateClassPair class_addMethodclass_addIvar objc_registerClassPair
● objc_disposeClassPair
Class cls = objc_allocateClassPair(MyClass.class, "MySubClass", 0);
class_addMethod(cls, @selector(submethod1), (IMP)imp_submethod1, "v@:");
class_replaceMethod(cls, @selector(method1), (IMP)imp_submethod1, "v@:");
class_addIvar(cls, "_ivar1", sizeof(NSString *), log(sizeof(NSString *)), "i");
objc_property_attribute_t type = {"T", "@\"NSString\""};
objc_property_attribute_t ownership = { "C", "" };
objc_property_attribute_t backingivar = { "V", "_ivar1"};
objc_property_attribute_t attrs[] = {type, ownership, backingivar};
class_addProperty(cls, "property2", attrs, 3);
objc_registerClassPair(cls);
id instance = [[cls alloc] init];
[instance performSelector:@selector(submethod1)];
[instance performSelector:@selector(method1)];
2014-10-23 11:35:31.006 RuntimeTest[3800:66152] run sub method 1
2014-10-23 11:35:31.006 RuntimeTest[3800:66152] run sub method 1
//
id class_createInstance ( Class cls, size_t extraBytes );
//
id objc_constructInstance ( Class cls, void *bytes );
//
void * objc_destructInstance ( id obj );
● class_createInstanceextraBytes ARC
class_createInstance +allocclass_createInstance NSString
id theObject = class_createInstance(NSString.class, sizeof(unsigned));
id str1 = [theObject init];
NSLog(@"%@", [str1 class]);
id str2 = [[NSString alloc] initWithString:@"test"];
NSLog(@"%@", [str2 class]);
2014-10-23 12:46:50.781 RuntimeTest[4039:89088] NSString
2014-10-23 12:46:50.781 RuntimeTest[4039:89088] __NSCFConstantString
class_createInstance NSString__NSCFConstantString ●objc_constructInstance (bytes)● objc_destructInstance
1. //
id object_copy ( id obj, size_t size );
//
id object_dispose ( id obj );
A BB AB AA BB BA
NSObject *a = [[NSObject alloc] init];
id newB = object_copy(a, class_getInstanceSize(MyClass.class));
object_setClass(newB, MyClass.class);
object_dispose(a);
2. //
Ivar object_setInstanceVariable ( id obj, const char *name, void *value );
//
Ivar object_getInstanceVariable ( id obj, const char *name, void **outValue );
// void * object_getIndexedIvars ( id obj );
//
id object_getIvar ( id obj, Ivar ivar );
//
void object_setIvar ( id obj, Ivar ivar, id value );
Ivar object_getIvarobject_getInstanceVariable object_setIvarobject_setInstanceVariable 3.
//
const char * object_getClassName ( id obj );
//
Class object_getClass ( id obj );
- run time
- run Time
- Compile time& link time&run time
- nokia web run time
- Run Time Error 10853
- C Run-time library
- Run-Time Library
- Run-Time Library
- Run-time PM 详解
- run-time exception
- Run-time PM 详解
- Run-time PM 详解
- Run-time PM 详解
- Run-time PM 详解
- Run-time PM 详解
- C run-time library
- Run-time PM 详解
- Run-time PM
- Android Studio 调试机器人App 出现红叉 问题
- Linux下的静态库、动态库和动态加载库
- HttpUtils请求网络+xlistview上拉刷新,下拉加载
- ASP.NET(C#)读取Excel的文件内容
- EXPRESS OLE DB Library for C++
- run Time
- Gitlab迁移小记
- 发现个学spark的地方,不错
- 关键字和宏
- C#学习入门
- Android版本更新示例
- SDUT3347数据结构实验之数组三:快速转置
- 7.29
- PHPCMS V9 列表页调用任意文章内容