简体   繁体   中英

How to build a xcode 9 project with Swift 4.0 using Pods in Swift 3?

I want the main module of my iOS App to compile Swift 4.0 while the CocoaPods module compiles swift 3.

PS: Using Xcode 9 beta 2.

If you are using some pods written in Swift 4, but some are Swift 3.2, here is the way you can specify the SWIFT_VERSION value for them:

swift_32 = ['Pod1', 'Pod2', 'Pod3'] # if these pods are in Swift 3.2
swift4 = ['Pod4', 'Pod5', 'Pod6'] # if these pods are in Swift 4

post_install do |installer|

    installer.pods_project.targets.each do |target|
        swift_version = nil

        if swift_32.include?(target.name)
            swift_version = '3.2'
        end

        if swift4.include?(target.name)
            swift_version = '4.0'
        end

        if swift_version
            target.build_configurations.each do |config|
                config.build_settings['SWIFT_VERSION'] = swift_version
            end
        end

    end

end

Finally I got it to work: All I had to do was to put this script in the end of Podfile:

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

Here is a far less verbose way to set the pods you need to 3.2 and leave all others at 4.0

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

Just modify the array in the if statement. everything else will default to 4.0

Project Navigator > Choose 'Pods' > Choose the Swift 3.2 Pod > 'Build Settings' > Scroll down and then set Swift Language Version to 3.2 in 'Swift Compiler - Language section'.

On doing this, Xcode will show one Buildtime issue. It will ask you to convert the source code of pods to Swift 4. Don't do that. Click on that issue > Uncheck 'Remind Me' > Click 'Convert Later'.

Project Navigator

项目导航器

Build Settings

构建设置

Set Swift 4.0 for all the targets except that framework which should be Swift 3.2

It's what I'm currently doing in a project

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM