Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the correct way to update swift version in Xcode/Cocoapods project?

I have an Xcode project, with cocoapods (RealmSwift) that was written in Swift 4.2.

I have since updated Xcode to 10.2, and am now being prompted to convert my code (and the RealmSwift code) to Swift 5, and a load of warnings are being generated for Realm Swift (i.e. 'public' modifier is redundant for instance method declared in a public extension).

What is the correct way to update the Cocoapods code? Do I need to specify/upgrade to a newer version of RealmSwift (and how would I know it was Swift 5 compatible?), or is there a recommended way of suppressing the warnings if I don't want to upgrade to Swift 5?

Any pointers to some documentation on the best way to handle xcode/swift/cocoapods updates would be appreciated.

like image 587
Lena V Avatar asked Apr 14 '19 17:04

Lena V


People also ask

How do I change the Swift version in Xcode?

You can find the Toolchains menu under Xcode menu, where you can easily switch between installed Swift toolchains. Toolchains menu.

How do I update a package in Swift?

Adding a Swift Package To add a new Swift package to your project, navigate to the Package Dependencies (<YOUR_PROJECT> > Package Dependencies) screen and click the +. At the top right of the new window, enter the URL of the Swift package and click Add Package.


1 Answers

When updating Xcode version you should just be sure you don't end up in a situation where you can no longer work.

Instead of updating Xcode you could just install two version of Xcode on the same mac and use both of them by trying out the new version.

For now, you can go back to your previous Xcode by downloading the previous version here https://developer.apple.com/download/more, extract the archive you will download and copy the App file to your Applications folder in macOS.

What I would suggest as a solution before you start upgrading everything, as you don't know how all the new pod updates will work with your code implementation, is to delete the Xcode 10.2 and go back install 10.1 or 10.0 (can download them from the previous link).

For later on, when your project builds fine and it's stable and you would like to try to upgrade everything (Xcode, Swift and PODs), you should first of all check if your project build with your current Swift version (which Swift version is your project currently set on you can find under Target > Build Settings > Swift Language Version).

At this moment your project doesn't build, so make your project build by using a previous Xcode version to make your life easier and also to be sure that by upgrading everything you don't end up with broken functionality from other PODs that might have changed the way their implementation works with your project.

When your projects build without errors and you want to upgrade to a new version of Swift

  • Research on the latest version of your PODs and which Swift version they support (go the project github pages, or in cocoapods)
  • Try to upgrade just your project without upgrading your PODs, if that works you are going to upgrade also your PODs later on, to ensure your PODs stays with the current Swift version that is working for you (let's say it is right now Swift 3.2 for example) you add this snippet to your 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
    
  • To convert just your project to the newer Swift version go to Edit -> Convert -> Convert to current swift syntax A popup will appear with a list of targets, including the pods. Deselect everything except your project target, the unit and UI tests and press convert. Wait for the project to build and generate the preview and apply the changes. Fix all the issues and warning created by the new Swift version requirements.

  • To update all the PODs that support the new Swift version you upgraded your project, do it by using the right POD version for each POD that has support to the newer Swift version and for the one that don't support yet the newer Swift version you could replace the code snippet on the Podfile with this

    post_install do |installer|
        installer.pods_project.targets.each do |target|
            if ['UnsupportedPod1', 'RxSwift', 'RxCocoa'].include? target.name
                target.build_configurations.each do |config|
                    config.build_settings['SWIFT_VERSION'] = '3.2'
                end
            end
        end
    end
    
like image 83
denis_lor Avatar answered Oct 21 '22 07:10

denis_lor