【问题标题】:Is it possible to introspect the array's type in Objective-C Generics -- Xcode 7+是否可以在 Objective-C 泛型中内省数组的类型 - Xcode 7+
【发布时间】:2015-09-11 11:09:09
【问题描述】:

我使用它来获取属性名称的类:

- (Class)gm_classForPropertyName:(NSString *)propertyName {
    objc_property_t prop = class_getProperty([self class], propertyName.UTF8String);
    const char * attr = property_getAttributes(prop);
    NSString *attributes = [NSString stringWithUTF8String:attr];
    NSArray *components = [attributes componentsSeparatedByString:@"\""];
    Class propertyClass;
    for (NSString *component in components) {
        Class class = NSClassFromString(component);
        if (class) {
            propertyClass = class;
            break;
        } else {
            class = swiftClassMapping[component];
            if (class) {
                propertyClass = class;
                break;
            }
        }
    }
    return propertyClass;
}

但这是重要的部分:

    objc_property_t prop = class_getProperty([self class], propertyName.UTF8String);
    const char * attr = property_getAttributes(prop);
    NSString *attributes = [NSString stringWithUTF8String:attr];

当我登录attributes 时,它仍然看起来像一个无类型的 NSArray 作为字符串。有什么方法可以自省类类型吗?

T@"NSArray",&,N,V_children

tl;博士

参加这门课:

@interface SomeClass : NSObject <GenomeObject>
@property (strong, nonatomic) NSArray<SomeOtherClass *> *children;
@end

鉴于类 SomeClass 和属性名称 children,我有没有办法找到 childrenSomeOtherClass 类型对象的数组?

【问题讨论】:

    标签: ios objective-c generics introspection objective-c-runtime


    【解决方案1】:

    根据 WWDC 关于此功能的讨论,它是使用类型擦除实现的。这意味着有关泛型的信息在编译期间被删除,并且在运行时不存在。

    这是相关视频:https://developer.apple.com/videos/wwdc/2015/?id=401lightweight generics 讨论从大约 22 分钟开始。在 27:20 他们解释说它是使用类型擦除实现的。

    【讨论】:

    • 很想知道,我正试图找出它是哪一个。
    猜你喜欢
    • 2011-08-18
    • 2011-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-16
    • 1970-01-01
    相关资源
    最近更新 更多