Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using CocoaPods mixed Swift3 and Swift4 pods

Using CocoaPods, most pods are still Swift3 pods, but when imported in Xcode, all pods defaults to Swift 4, which makes the compilation step fail.

How can I use Swift4 pods with legacy Swift3 pods in my Podfile without having to manually changed the target swift version for all my pods?

like image 733
Claude Houle Avatar asked Jan 01 '26 15:01

Claude Houle


2 Answers

Cocoapod (as of v1.3.1) is yet to support mixing Swift 3.2 and Swift 4.

A workaround is possible. Append this to your Podfile, and add pods that are already Swift 4 to the array swift4Targets, then pod install.

# Workaround Cocoapods to mix Swift 3.2 and 4
# Manually add to swift4Targets, otherwise assume target uses Swift 3.2
swift4Targets = ['MyTarget1', 'MyTarget2']
post_install do |installer|
    installer.pods_project.targets.each do |target|
        target.build_configurations.each do |config|
            if swift4Targets.include? target.name
                config.build_settings['SWIFT_VERSION'] = '4'
            else
                config.build_settings['SWIFT_VERSION'] = '3.2'
            end
        end
    end
end
like image 113
samwize Avatar answered Jan 03 '26 14:01

samwize


iOS 11 is about to get released, you want to test your app with Xcode9 and you will encounter this little (but important) problem: Not all pods will be Swift 4 on day 1 (or will ever be!)

In your private pods (or public pods that are open-source), you should add:

Pod::Spec.new do |s|
    s.pod_target_xcconfig = { 'SWIFT_VERSION' => '3.2' }
    s.compiler_flags = '-swift-version 3.2'
end

Anybody importing those pods will get the correct Swift version out of the box, without any special thing on their part.

I wish I could just modify all podspecs, but I do not have that power. But that can be fixed in your Podfile, just make all your pods defaults to Swift 3.2


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

However, you may be like me, and split your apps with private pods (code reuse is good!), and you will probably convert those pods to Swift 4. And now you want to mix both types



Again, in your podfile, use this:

post_install do |installer|
    ['TargetName1','TargetName2','TargetName3'].each do |targetName|
        targets = installer.pods_project.targets.select { |target| target.name == targetName }
        target = targets[0]
        target.build_configurations.each do |config|
            config.build_settings['SWIFT_VERSION'] = '4.0'
        end
    end
end

You're done! Eventually, all your pods will have migrated to Swift4, and you can just go back to using CocoaPods normally


like image 39
Claude Houle Avatar answered Jan 03 '26 13:01

Claude Houle



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!