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

旧城冷巷雨未停 提交于 2019-11-28 04:40:45
Justin Miller

I found this while searching how to set the SWIFT_VERSION to 3.2 for Xcode 9.

Using the AirMapSDK which itself and multiple dependencies need 3.2 instead of 4.0 Here is an example of how to set pod specific SWIFT_VERSION for others that may come across this question.

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

You can change the if array to specify whatever pod you need to set the version too.

Edmund Lee

If you need to manage multiple Swift versions for pods, you can build a mapping.

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

Details

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

Ruby code in podfile

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

I was using this solution in CocoaPods 1.6.0.beta.2 until I realised that it does not work for iOS & tvOS projects. For example in a project using PromiseKit target.name can be PromiseKit-iOS or PromiseKit-tvOS and it is useless to check if "PromiseKit" contains such substring.

I can provide a better solution. Declare a hash with Swift versions as keys and libraries names as values:

my_project_pods_swift_versions = Hash[
  "3.0", ["PagingMenuController", "TCPickerView"],
  "4.0", ["PromiseKit"]
]

Use these functions to check if target.name contains a string like "PromiseKit", but not vice versa:

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

You should call setup_all_swift_versions inside post_install:

post_install do |installer|
  installer.pods_project.targets.each do |target|
    setup_all_swift_versions(target, my_project_pods_swift_versions)
  end
end

Here you can find all these code snippets unified into a single code block.

Update for cocoapods 1.7+ if you have enabled multiple xcodeproj generation:

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
Rokon

You can define an array to set Swift version nicely for all pods inside that array:

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!