swift3.0  利用运行时机制查看所有的属性名称

前端之家收集整理的这篇文章主要介绍了swift3.0  利用运行时机制查看所有的属性名称前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

swift3.0 利用运行时机制查看所有的属性名称

var count : UInt32 = 0
        let ivars = class_copyIvarList(UIGestureRecognizer.self,&count)!
        for i in 0..<count {
            let ivar = ivars[Int(i)]
            let name = ivar_getName(ivar)
            print(String(cString: name!))
        }

OC版本的也增加上

- (NSArray *)filterPropertys
    {
        NSMutableArray *props = [NSMutableArray array];
        unsigned int outCount,i;
        objc_property_t *properties = class_copyPropertyList([self class],&outCount);
        for (i = 0; i<outCount; i++)
        {
            objc_property_t property = *properties;
            const char* char_f =property_getName(property);
            NSString *propertyName = [NSString stringWithUTF8String:char_f];
            NSLog(@"%@",propertyName);
            
        }
        free(properties);
        return props;
    }
原文链接:https://www.f2er.com/swift/322803.html

猜你在找的Swift相关文章