Xcode - Upgrade to Swift 4 Manually

XCode - upgrade to Swift 4 manually

I have updated the "Swift Language Version" in Build Settings of the second target to Swift 4.0 manually

Check the Swift 3 @objc inference is set to off.

Other than that, you are good to go.

How to migrate a code from swift 3 to swift 4 or above?

You cannot run swift 3 on Xcode 11. Download a version of XCode 10.1,You can run both.There will be option to migrate code to swift 4.

here you can download XCode 10.1:

XCode 10.1

Please check this link below. You can upgrade to swift 4.

https://medium.com/@hanif.awan2007/tips-upgrading-your-code-from-swift-3-to-swift-4-dbcba128b48b

How can change Swift 3 to 4 in Xcode 11.4

There are 2 ways you can do the migration

  1. Use Xcode 10.1's migration tool to update your code-base directly to swift4
    But this will still require manual pod update

  2. If your code-base is small, you can skip the hassle of downloading and installing of Xcode and could manually update code to swift 4 by refactoring.

Follow this tutorial, It is comprehensive enough for migration

How to update a cocoapod built in Swift 3 to Swift 4

Go to your Pods project, then:

  1. Select the target you want to update
  2. Go into its build settings
  3. Select Swift 3.2 as the language version

Sample Image

iOS: Migration (Upgrade) from Swift 3 to Swift 4

We have a better option to fix this error at once. It can be solved out from Editor menu

Sample Image

Use this option to fix all the error at once in class.

Note: You cannot always rely on Xcode suggestions, but for migration case it will work in most of the case.

Update to Xcode 9 and Swift 4

Upgrading Xcode version, will not change/upgrade your Swift programming
language version. These warning show, your current Swift programming language version is not 4.0 (it is below 4.0 i.e. 3.2).

(Also note, Xcode 9 and 8.3 both support Swift 3.2. You can work with Xcode 9 using Swift 3.2 also.)

So, if you open your Xcode 8.x project in Xcode 9, will work with Swift version 3.2.

Manually you need to change Swift 3.2 to 4.0 from project build settings.

Here is - How to see current Swift language version and change it to newer.

Sample Image

Also note: Once you change your swift language ensure following steps:

  • You must update/upgrade your cocoa pod (CocoaPods)
  • Update your project's pod libs/files (using command pod install or pod update)

For easier migration refer these answers:

  • Xcode 9 Swift Language Version (SWIFT_VERSION)

(Xcode) Menus: Edit ► Covert ► To Current Swift Syntax

Sample Image

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

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


Related Topics



Leave a reply



Submit