Claude Houle
Claude Houle

Reputation: 42876

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?

Upvotes: 2

Views: 2070

Answers (3)

Naloiko Eugene
Naloiko Eugene

Reputation: 2636

I did it this way. So for now all pods, except 2 are using Swift 3.2.

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

    installer.pods_project.targets.each do |target|
        if ['MapboxCoreNavigation', 'MapboxNavigation'].include? target.name
            target.build_configurations.each do |config|
                config.build_settings['SWIFT_VERSION'] = '4.0'
            end
        end
    end
end

Upvotes: 1

samwize
samwize

Reputation: 27383

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

Upvotes: 6

Claude Houle
Claude Houle

Reputation: 42876

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


Upvotes: 3

Related Questions