You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Podfile脚本中获取当前Xcode版本以适配不同环境?

适配不同Xcode版本的Podfile条件脚本实现

实现思路

通过命令行获取当前Xcode版本号,添加条件判断逻辑,仅在Xcode 15.0及以上版本(对应Sonoma 14.0系统适配需求)时,才注入所需的预编译宏配置,兼容Xcode 14.3的开发环境。

完整Podfile脚本示例

# 获取当前Xcode版本并转换为可比较的数值
def xcode_version
  version_string = `xcodebuild -version | grep "Xcode" | awk '{print $2}'`
  version_string.strip.to_f
end

post_install do |installer|
  installer.pods_project.targets.each do |target|
    target.build_configurations.each do |config|
      # 仅在Xcode 15.0及以上版本添加预编译宏
      if xcode_version >= 15.0
        config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)', '_LIBCPP_ENABLE_CXX17_REMOVED_UNARY_BINARY_FUNCTION']
      end
    end
  end
end

代码说明

  • xcode_version方法:通过xcodebuild -version命令提取Xcode版本号,转换为浮点数后方便版本比较。
  • 条件判断:确保只有使用Xcode 15.0+的环境才会执行预编译宏的添加操作,Xcode 14.3环境不受影响,避免不必要的配置冲突。

内容的提问来源于stack exchange,提问作者Sal Jeong

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 01:47:37