我怎样才能找出Objective-C的泛型类型? [英] How can I find out the Objective-C generics type?
问题描述
NSArray
返回值和属性定义一个类型提示,定义如 NSArray< UIImage>
。 我想用这个特性来重写我的JSON解串器类类型提示 - 以前我已经通过向具有同类数组作为属性的类添加 - (Class)jsonHintForKey:(NSString *)key
>来解决此问题。 p>
你知道我是否可以使用Objective-C运行时在运行时获得这种新类型提示的类吗?
Xcode 7中引入的轻量级泛型只是编译时间提示,以帮助编译器提出警告,但在运行时您会得到与变量只是 NSArray
s id
s。
资料来源: WWDC'1 5Swift和Objective-C互操作性会话
请参阅谈话记录:
lockquote
所以整个轻量级泛型功能基于类型擦除模型。这意味着编译器拥有所有这些丰富的静态类型信息,但是它会在生成代码时擦除该信息。
Xcode 7 contains an Objective-C variant, where you can define a type-hint for (homogenous) NSArray
return values and properties, defined such as NSArray<UIImage*>
.
I'd like to use this feature to rewrite my JSON-deserializer class (which needs such kind of type hints – previously I have solved this by adding a -(Class)jsonHintForKey:(NSString*)key
to my classes that have homogenous arrays as properties.)
Do you know whether (and if so, how) I can use the Objective-C runtime to get the class of this new type hint at runtime?
The lightweight generics introduced in Xcode 7 are just compile time hints to help the compiler raise warnings, but at run time you get the same old behavior with your variable being just NSArray
s of id
s.
Source: WWDC '15 "Swift and Objective-C Interoperability" session
See the transcript of the talk:
So the entire lightweight generics feature is based on a type erasure model. Which means that the compiler has all of this rich static type information but it erases that information when generating code.
这篇关于我怎样才能找出Objective-C的泛型类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!