iOSランタイム(4)-共通関数

4403 ワード

1クラス


1.1オブジェクトの作成

 id class_createInstance(Class cls, size_t extraBytes)

eg:
size_t size = class_getInstanceSize([Person class]);
Person *person = class_createInstance([Person class], size);

1.2クラス名の取得

const char *class_getName(Class cls) 

eg
const char *name = class_getName([Person class])

1.3親の取得

Class class_getSuperclass(Class cls)

eg
Class class = class_getSuperclass([Person class]);

1.4クラスのサイズの取得

size_t class_getInstanceSize(Class cls)

2メンバー変数


2.1メンバー・リストの取得

Ivar *class_copyIvarList(Class cls, unsigned int *outCount)

eg:
  unsigned int count;
  Ivar *ivarList = class_copyIvarList([self class], &count);
  for (int i = 0; i < count; i++) {
      Ivar ivar = ivarList[i];
      //  
      NSString *name = [NSString stringWithUTF8String:ivar_getName(ivar)];
      NSString *type = [NSString stringWithUTF8String:ivar_getTypeEncoding(ivar)];
      NSLog(@"%@%@", type, name);
  }

3プロパティ


3.1メンバー・リストの取得

objc_property_t *class_copyPropertyList(Class cls, unsigned int *outCount)

eg:
  unsigned int count;
  objc_property_t *propertyList = class_copyPropertyList([self class], &count);
  for (int i = 0; i < count; i++) {
      objc_property_t property = propertyList[i];
      //  
      NSString *name = [NSString stringWithUTF8String:property_getName(property)];
      NSString *type = [NSString stringWithUTF8String:property_getAttributes(property)];
      NSLog(@"%@%@", type, name);
  }

4メソッド


4.1インスタンスオブジェクトの取得方法

Method class_getInstanceMethod(Class cls, SEL name)

eg
Method method = class_getInstanceMethod([self class], @selector(methodName));

4.2クラスの取得方法

Method class_getClassMethod(Class cls, SEL name)

eg
Method method = class_getClassMethod([self class], @selector(methodName));

4.3取得方法の関数ポインタ(IMP)

IMP class_getMethodImplementation(Class cls, SEL name)

eg:
IMP imp = class_getMethodImplementation([self class], @selector(methodName));

4.4メソッドリストの取得

Method *class_copyMethodList(Class cls, unsigned int *outCount)
 unsigned int count;
  Method *methodList = class_copyMethodList([self class], &count);
  for (int i = 0; i < count; i++) {
      Method method = methodList[i];
      NSLog(@"%s%s", __func__, sel_getName(method_getName(method)));
  }

4.5増加方法

BOOL class_addMethod(Class cls, SEL name, IMP imp, const char *types)
 class_addMethod(theClass, selector,  method_getImplementation(method),  method_getTypeEncoding(method));

4.6メソッド置換

IMP class_replaceMethod(Class cls, SEL name, IMP imp, 
                                    const char *types) 
 class_replaceMethod(class, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));

4.7方法交換

+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Class class = [self class];

        SEL originalSelector = @selector(viewWillAppear:);
        SEL swizzledSelector = @selector(bx_viewWillAppear:);

        Method originalMethod = class_getInstanceMethod(class, originalSelector);
        Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);

        BOOL success = class_addMethod(class, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));
        if (success) {
            class_replaceMethod(class, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
        } else {
            method_exchangeImplementations(originalMethod, swizzledMethod);
        }
    });
}