How to set the Legacy Swift Version for each Pod in Podfile Xcode 9.0 Swift 3.2 / Swift 4.0

时间:2016-11-09 06:51:45

标签: swift cocoapods podfile

I am currently setting the legacy in the Podfile to SWIFT_VERSION = 2.3, but some of the libraries I am using are Swift 3.0, which means that I need to manually set the legacy for all Swift 3.0 pods legacy to No on each pod install. How do I configure each pod version in the Podfile installer?

This is what I am setting:

post_install do |installer|
 installer.pods_project.targets.each do |target|
     target.build_configurations.each do |config|
         config.build_settings['SWIFT_VERSION'] = '2.3'
     end
 end
end

If I set config.build_settings['SWIFT_VERSION'] = '3.0', than we have issues with Swift 2.3 pods. The best solution is if we set each pod legacy version separately to avoid having to manually set it.

8 个答案:

答案 0 :(得分:80)

我在搜索如何将XIFT 9的SWIFT_VERSION设置为3.2时找到了这个。

使用AirMapSDK本身和多个依赖项需要3.2而不是4.0以下是如何为可能遇到此问题的其他人设置pod特定SWIFT_VERSION的示例。

post_install do |installer|
  installer.pods_project.targets.each do |target|
    if ['AirMapSDK', 'PhoneNumberKit', 'Lock', 'RxSwift', 'RxSwiftExt', 'RxCocoa', 'RxDataSources', 'ProtocolBuffers-Swift'].include? target.name
      target.build_configurations.each do |config|
        config.build_settings['SWIFT_VERSION'] = '3.2'
      end
    end
  end
end

您可以更改if数组以指定设置版本所需的任何pod。

答案 1 :(得分:4)

如果您需要管理Pod的多个Swift版本,则可以构建映射。

DEFAULT_SWIFT_VERSION = '4'
POD_SWIFT_VERSION_MAP = {
  'Dotzu' => '3'
}

post_install do |installer|
  installer.pods_project.targets.each do |target|
    swift_version = POD_SWIFT_VERSION_MAP[target.name] || DEFAULT_SWIFT_VERSION
    puts "Setting #{target.name} Swift version to #{swift_version}"
    target.build_configurations.each do |config|
      config.build_settings['SWIFT_VERSION'] = swift_version
    end
  end
end

答案 2 :(得分:1)

如果您想使用其他Swift版本,则可以使用以下方法:

def pods

  pod 'PodWithSwift40'
  pod 'Pod1WithSwift42'
  pod 'Pod2WithSwift42'

end

post_install do |installer|
  installer.pods_project.targets.each do |target|
    if ['Pod1WithSiwft42', 'Pod2WithSiwft42'].include? target.name
        target.build_configurations.each do |config|
            config.build_settings['SWIFT_VERSION'] = '4.2'
        end
    else
        target.build_configurations.each do |config|
            config.build_settings['SWIFT_VERSION'] = '4.0'
        end
    end
  end
end

答案 3 :(得分:0)

我在CocoaPods 1.6.0.beta.2中使用了screenshot,直到我意识到它不适用于iOS和tvOS项目。例如,在使用this solution 'The text I actually need.' 的项目中,可以是target.namePromiseKit-iOS,并且检查PromiseKit-tvOS是否包含这样的子字符串是没有用的。

我可以提供更好的解决方案。使用Swift版本作为键声明一个散列,并使用值作为库名声明一个散列:

"PromiseKit"

使用以下函数检查my_project_pods_swift_versions = Hash[ "3.0", ["PagingMenuController", "TCPickerView"], "4.0", ["PromiseKit"] ] 是否包含类似target.name的字符串,反之则不:

"PromiseKit"

您应该在def setup_all_swift_versions(target, pods_swift_versions) pods_swift_versions.each { |swift_version, pods| setup_swift_version(target, pods, swift_version) } end def setup_swift_version(target, pods, swift_version) if pods.any? { |pod| target.name.include?(pod) } target.build_configurations.each do |config| config.build_settings['SWIFT_VERSION'] = swift_version end end end 内部致电setup_all_swift_versions

post_install

PromiseKit,您可以将所有这些代码段统一到一个代码块中。

答案 4 :(得分:0)

详细信息

  • CocoaPods v1.6.1
  • Xcode 10.2.1(10E1001)

podfile中的Ruby代码

swift_versions_of_pods = { 'swiftScan' => '4.0', 'GRDB.swift' => '4.2' }
post_install do |installer|
  installer.pods_project.targets.each do |target|
    defined_swift_version = swift_versions_of_pods[target.name]
    next if defined_swift_version.blank?
    target.build_configurations.each do |config|
      config.build_settings['SWIFT_VERSION'] = defined_swift_version
    end
  end
end

答案 5 :(得分:0)

如果已启用多个xcodeproj生成,请更新cocoapods 1.7 +:

install! 'cocoapods', :generate_multiple_pod_projects => true

<Pod list section>

post_install do |installer|
    installer.pod_target_subprojects.each do |subproject|
        subproject.targets.each do |target|
            if target.name == 'OldSwiftPod'
                target.build_configurations.each do |config|
                    config.build_settings['SWIFT_VERSION'] = '3.2'
                end
            end
        end
    end
end

答案 6 :(得分:0)

如果您想将旧版Swift版本仅设置为一个pod:

post_install do |installer|
  target = installer.pods_project.targets.detect {|e| e.name == 'AirMapSDK'}
  unless target.nil?
  target.build_configurations.each do |config|
  config.build_settings['SWIFT_VERSION'] = '3.2'
    end
  end
end

答案 7 :(得分:-1)

您可以定义一个数组以很好地为该数组内的所有Pod设置Swift版本:

post_install do |installer|
 SWIFT_3_2_PODS = %w['AirMapSDK', 'PhoneNumberKit', 'Lock', 'RxSwift', 'RxSwiftExt', 'RxCocoa', 'RxDataSources', 'ProtocolBuffers-Swift']

 installer.pods_project.targets.each do |target|
  if SWIFT_3_2_PODS.include? target.name
      target.build_configurations.each do |config|
          config.build_settings['SWIFT_VERSION'] = '3.2'
       end
    end
  end
end