3

我们使用 [ClassName class] 来检查新版本 iOS 中的弱链接类或类。[ClassName class] 的底层发生了什么?它会通过 NSClassFromString 吗?

4

2 回答 2

2

请记住,在 obj-c 中,您可以向 nil 发送消息。

我怀疑正在发生的事情是 ClassName 在运行时加载之前为零。如果它从未加载,那么您基本上将类消息发送到 nil,根据文档将返回 0/nil。

根据文档

这是有效的,因为如果弱链接类不可用,向它发送消息就像向 nil 发送消息一样。如果您对弱链接类进行子类化并且超类不可用,则子类也将显示为不可用。

重要的是,框架必须支持NS_CLASS_AVAILABLE(10_5, 2_0)其类上的宏。

于 2013-10-22T23:53:06.243 回答
0

A few things going on here. Weak linking just tells LLVM (-weak_framework <framework_name>) to not worry about the symbol along with optimizations. Apple recommends enabling this option when supporting previous not future run-time SDKs. You can't weak link to a framework that is not present in the SDK.

As for [ClassName class] it's not a NSString nor the toll-free bridge counterpart CFStringRef. It's the Objective-C class metadata

typedef struct objc_object {
    Class isa;
} *id;

Identity is tested via the NS_CLASS_AVAILABLE macro which scans all objects that derive from the run-time metadata.

Apple Doc Ref https://developer.apple.com/library/ios/documentation/DeveloperTools/Conceptual/cross_development/Using/using.html#//apple_ref/doc/uid/20002000-SW3

于 2013-10-23T00:03:49.500 回答