0
点赞
收藏
分享

微信扫一扫

ios类和对象操作函数

点亮自己的那盏灯 2022-02-17 阅读 51

ios类和对象操作函数

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

@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

operate:

- (void)viewDidLoad {
    [super viewDidLoad];
    // ios类和对象操作函数
    MyClass *myClass = [[MyClass alloc] init];
    unsigned int outCount = 0;
    Class cls = myClass.class;
    
    NSLog(@"class name: %s", class_getName(cls)); // 类名
    NSLog(@"super class name: %s", class_getName(class_getSuperclass(cls))); // 父类
    NSLog(@"MyClass is %@ a meta-class", (class_isMetaClass(cls) ? @"yes" : @"not")); // 是否元类
    
    Class meta_class = objc_getMetaClass(class_getName(cls));
    NSLog(@"%s's meta-class is %s", class_getName(cls), class_getName(meta_class));
    NSLog(@"instance size: %zu", class_getInstanceSize(cls)); // 变量实例大小
    
    // 获取成员变量
    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(@"instance variable %s", ivar_getName(string));
    }
    
    // 属性操作
    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));
    }
    
    // 方法操作
    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 %@ response to selector: method3WithArg1:arg2:", class_respondsToSelector(cls, @selector(method3WithArg1:arg2:)) ? @"yes" : @"not");
    
    IMP imp = class_getMethodImplementation(cls, @selector(method1));
    imp();
    
    // 协议
    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 %@ response to protocol %s", class_conformsToProtocol(cls, protocol) ? @"yes" : @"not", protocol_getName(protocol));
}

output:

2022-02-16 23:56:40.772652+0800 Test[56833:11021466] class name: MyClass
2022-02-16 23:56:40.772701+0800 Test[56833:11021466] super class name: NSObject
2022-02-16 23:56:40.772724+0800 Test[56833:11021466] MyClass is not a meta-class
2022-02-16 23:56:40.772747+0800 Test[56833:11021466] MyClass's meta-class is MyClass
2022-02-16 23:56:40.772766+0800 Test[56833:11021466] instance size: 48
2022-02-16 23:56:40.772786+0800 Test[56833:11021466] instance variable's name: _instance1 at index: 0
2022-02-16 23:56:40.772804+0800 Test[56833:11021466] instance variable's name: _instance2 at index: 1
2022-02-16 23:56:40.772821+0800 Test[56833:11021466] instance variable's name: _array at index: 2
2022-02-16 23:56:40.772837+0800 Test[56833:11021466] instance variable's name: _string at index: 3
2022-02-16 23:56:40.772853+0800 Test[56833:11021466] instance variable's name: _integer at index: 4
2022-02-16 23:56:40.772870+0800 Test[56833:11021466] instance variable _string
2022-02-16 23:56:40.772886+0800 Test[56833:11021466] property's name: integer
2022-02-16 23:56:40.772904+0800 Test[56833:11021466] property's name: array
2022-02-16 23:56:40.772920+0800 Test[56833:11021466] property's name: string
2022-02-16 23:56:40.772936+0800 Test[56833:11021466] property array
2022-02-16 23:56:40.772952+0800 Test[56833:11021466] method's signature: method1
2022-02-16 23:56:40.772976+0800 Test[56833:11021466] method's signature: method2
2022-02-16 23:56:40.773216+0800 Test[56833:11021466] method's signature: method3WithArg1:arg2:
2022-02-16 23:56:40.773334+0800 Test[56833:11021466] method's signature: array
2022-02-16 23:56:40.773442+0800 Test[56833:11021466] method's signature: setArray:
2022-02-16 23:56:40.773543+0800 Test[56833:11021466] method's signature: string
2022-02-16 23:56:40.773650+0800 Test[56833:11021466] method's signature: setString:
2022-02-16 23:56:40.773753+0800 Test[56833:11021466] method's signature: integer
2022-02-16 23:56:40.773881+0800 Test[56833:11021466] method's signature: setInteger:
2022-02-16 23:56:40.774018+0800 Test[56833:11021466] method's signature: .cxx_destruct
2022-02-16 23:56:40.774132+0800 Test[56833:11021466] method method1
2022-02-16 23:56:40.774249+0800 Test[56833:11021466] class method: classMethod1
2022-02-16 23:56:40.774394+0800 Test[56833:11021466] MyClass is yes response to selector: method3WithArg1:arg2:
2022-02-16 23:56:40.774497+0800 Test[56833:11021466] call method method1
2022-02-16 23:56:40.774612+0800 Test[56833:11021466] protocol name: NSCopying
2022-02-16 23:56:40.774691+0800 Test[56833:11021466] protocol name: NSCoding
2022-02-16 23:56:40.774924+0800 Test[56833:11021466] MyClass is yes response to protocol NSCoding
举报

相关推荐

0 条评论