Xcode 和可选框架

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33038137/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 08:01:28  来源:igfitidea点击:

Xcode and optional frameworks

iosxcodemacos

提问by Jeef

Under Linked frameworks and librariesthere is a Requiredor Optionaloption.

链接的框架和库下,有一个必需可选的选项。

Could somebody explain a situation in which and how to work with an optional framework? I could see hypothetically a situation where I have some test data and IFthe framework is included I would want to enable some sort of functionality and if it is not included than perhaps I wouldn't do something....

有人可以解释一下使用可选框架以及如何使用可选框架的情况吗?假设我可以看到一种情况,我有一些测试数据,如果包含框架,我想启用某种功能,如果不包含它,我可能不会做某事....

But otherwise I'm at a loss as to when you would want to use an optional framework

但除此之外,我不知道您何时想要使用可选框架

(a code example would be awesome if one exists)

(如果存在代码示例会很棒)

回答by Tamás Zahola

Optional linking is useful if you're targeting older OS versions where a certain framework might not be available yet. In this case you can set the linkage of the given framework to optional, and this causes the program not to crash on launch if dlopencannot find the given framework.

如果您的目标是某个框架可能尚不可用的旧操作系统版本,则可选链接很有用。在这种情况下,您可以将给定框架的链接设置为可选,如果dlopen找不到给定框架,这会导致程序在启动时不会崩溃。

Then in your code you can put guard statements around the usage of this framework in order to avoid crashing b/c of using unresolved symbols:

然后在您的代码中,您可以在此框架的使用周围放置保护语句,以避免使用未解析的符号导致 b/c 崩溃:

 if (MyWeakLinkedFunction != NULL)
 {
     result = MyWeakLinkedFunction(); // this function comes from a weakly/optionally linked framework
 }

See: Frameworks and Weak Linking

请参阅:框架和弱链接