我正在开发一个 cocoa bean 。使用我的 pod 的人可以选择是否在他们的 Podfile
中包含此行:
use_frameworks!
如果他们包含它,我的代码将被编译成
.framework
文件。如果没有,就会编译成.a
文件。我想使用预处理器宏在我的 cocoa pod 中包含不同的代码,具体取决于
use_frameworks!
存在。例如,这样的事情:
#if PODFILE_CONTAINS_USE_FRAMEWORKS
@import My_Library;
#else
#import <My-Library/MyLibrary.h>
#end
当您的 pod 名称不是
c99extidentifier
时,就会出现这种情况。合规。我已经尝试检查
#if defined COCOAPODS
,但这在这两种情况下都有定义。
最佳答案
得到它的工作。
这个片段需要放在 Podfile 中:
post_install do |pi|
pi.pods_project.targets.each do |target|
target.build_configurations.each do |config|
unless pi.podfile.defined_in_file.read().include? "\nuse_frameworks!\n"
config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)']
config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] << 'NO_USE_FRAMEWORKS=1'
end
end
end
end
然后在源文件中,代码如下所示:
#if defined NO_USE_FRAMEWORKS
#import <My-Library/MyLibrary.h>
#else
@import My_Library;
#endif
默认为
@import
如果符号不存在,则语法。所以 Podfile 片段只有在
use_frameworks!
时才真正需要添加不存在。但无论如何它都会起作用。
关于cocoapods - 如何检测use_frameworks!在编译时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36799759/