How to Switch an Xcode Project to Use Swift Version 1.2 in the Xcode 7 Beta

How to migrate my swift 1.2 project into 2.0?

In the new Xcode 7 beta go to the Edit menu -> Convert -> To Latest Swift Syntax

This will run the code converter for you and show you the changes it is going to make. These are automatic changes (like changing println to print and so on).

Then to refactor the code to make it more Swift-like here are some tips:

  • Ensure you are using the new error handling functionality wherever possible (the code conversion tool does this for the most part but sometimes it gets it wrong).

  • Use guard statements where appropriate. In general use it to reduce indentation and nested if statements. These are really nice when used properly.

  • Almost all your global functions can be refactored into protocol extensions. Move generic functions to extensions.

  • When converting to/from a type (for instance String -> NSData and vice versa) use failable initializers with the parameter as the type to convert from instead of having properties on the type. So instead of doing someString.dataUsingEncoding(NSUTF8StringEncoding) do something like NSData(someString, encoding: NSUTF8StringEncoding). Note this is not how the API is implemented but I used it as an example to show how things can be more "Swifty".

  • Use availability checking where useful.

  • Move clean up code to defer blocks as much as possible. This can help redundant duplicated clean up code like file closing, etc.

XCode set to use Swift 2.0 by default?

The release version of Xcode (6.4) only supports Swift 1.2. You need to get the latest beta of Xcode 7 (beta 6). Once you have installed that (and you use it instead of the 6.x version) it will automatically use Swift 2.0.

You may also need to run xcode-select from the command line to switch the command line tools to use Swift 2.

xcode-select --switch /path/to/Xcode7/beta

You might need to run it with sudo

How to change Xcode setting to use Swift 1.1?

You can download the older version of Xcode on https://developer.apple.com/downloads/ all you need is to create an developer account.

How do I see which version of Swift I'm using?

Project build settings have a block 'Swift Compiler - Languages', which stores information about Swift Language Version in key-value format. It will show you all available (supported) Swift Language Version for your Xcode and active version also by a tick mark.

Project ► (Select Your Project Target) ► Build Settings ► (Type
'swift_version' in the Search bar) Swift Compiler Language ► Swift Language
Version ► Click on Language list to open it (and there will be a tick mark on any one of list-item, that will be current swift version).

Look at this snapshot, for easy understanding:

xcode with described areas highlighted


With help of following code, programmatically you can find Swift version supported by your project.

#if swift(>=5.7)
print("Hello, Swift 5.7")

#elseif swift(>=5.6)
print("Hello, Swift 5.6")

#elseif swift(>=5.5)
print("Hello, Swift 5.5")

#elseif swift(>=5.4)
print("Hello, Swift 5.4")

#elseif swift(>=5.3)
print("Hello, Swift 5.3")

#elseif swift(>=5.2)
print("Hello, Swift 5.2")

#elseif swift(>=5.1)
print("Hello, Swift 5.1")

#elseif swift(>=5.0)
print("Hello, Swift 5.0")

#elseif swift(>=4.2)
print("Hello, Swift 4.2")

#elseif swift(>=4.1)
print("Hello, Swift 4.1")

#elseif swift(>=4.0)
print("Hello, Swift 4.0")

#elseif swift(>=3.2)
print("Hello, Swift 3.2")

#elseif swift(>=3.0)
print("Hello, Swift 3.0")

#elseif swift(>=2.2)
print("Hello, Swift 2.2")

#elseif swift(>=2.1)
print("Hello, Swift 2.1")

#elseif swift(>=2.0)
print("Hello, Swift 2.0")

#elseif swift(>=1.2)
print("Hello, Swift 1.2")

#elseif swift(>=1.1)
print("Hello, Swift 1.1")

#elseif swift(>=1.0)
print("Hello, Swift 1.0")

#endif

Here is result using Playground (with Xcode 11.x)

Sample Image

Can I write Apps in Swift 1 (For iOS 7 and 8) using Xcode 7?

There will be errors if you attempt to run swift 1 in xcode 7, and yes, swift 2 will run on IOS 7-9

Update to Xcode 7 from Xcode 6

Everything will work fine, in Xcode 7 just do the following to convert from swift 1.2 to 2.0, From Xcode menu go to:

Edit> Convert > to Latest Swift Syntax.

Good luck !

How to update compiler to Swift 2?

This happens when you have different copies of Xcode IDE installed

Use xcode-select command line utility to select the version of Xcode that you wanna use

https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/xcode-select.1.html

$ xcode-select --switch /path/to/the/Xcode


Related Topics



Leave a reply



Submit