72

Is it possible to use Swift 2.2 in Xcode 8?

From Xcode 8 release notes:

"Xcode 8 supports switching toolchains, such as those from swift.org, without relaunching Xcode. (23135507)"

I have been trying to find swift 2.2 toolchain on swift.org website but couldn't. Have any one success in configuring Xcode 8 to work with swift 2.2

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
salabaha
  • 2,468
  • 1
  • 17
  • 18
  • What's the problem with migrating to 2.3? – kennytm Jun 15 '16 at 12:34
  • Want to try developing using Xcode 8 but still need my code to be compatible with Xcode 7 in order to make release builds. – salabaha Jun 15 '16 at 12:38
  • 1
    Swift 2.2 in Xcode 7 and Swift 2.3 in Xcode 8 are compatible. – Jim Jun 15 '16 at 12:40
  • 1
    I wouldn't say compatible. NONE of my auto layout constraints on my storyboards work at all in Xcode 8. I'm going to end up having to redo the entire layout for my apps. – DiggyJohn Sep 17 '16 at 14:28
  • @DiggyJohn I also have auto layout compatibility problem with Xcode 8.0. Does anyone know whether doing auto layout from scratch is the only solution? – Murat Yasar Sep 17 '16 at 23:21

5 Answers5

57

If you want to continue to use Swift 2 in Xcode 8, then use Swift 2.3 by enabling the Use Legacy Swift Language Version build setting (SWIFT_VERSION = 2.3).

Jim
  • 72,985
  • 14
  • 101
  • 108
  • I am just curious: Did you find a way to specify Swift 2.3 (Legacy) when *creating* a new project? – Martin R Jun 15 '16 at 12:45
  • @EricD: I think they are slightly different, but not in incompatible ways. No official source though. – Jim Jun 15 '16 at 12:47
  • @MartinR: New projects work the same way as old ones – just change the build setting. – Jim Jun 15 '16 at 12:47
  • 4
    @EricD: https://swift.org/blog/swift-2-3/: "Swift 2.2.1 and Swift 2.3 are very similar, with key changes being related to SDK differences." – Martin R Jun 15 '16 at 12:48
  • 1
    @Jim: Yes I know. But when I create a new iOS application in Xcode 8 and switch the build settings to "Legacy Swift" afterwards, all the generated template code needs to be fixed. – Martin R Jun 15 '16 at 12:51
  • 2
    I did notice that some items were bridged over to Swift like `NSURL`. In their Swift counterparts they were marked as optional so I had to force cast them. When I switched back to Xcode 7.3 it required me to remove the force cast. – keji Jun 15 '16 at 13:42
  • @kezi: I have notice the same behavior in other Foundation API as well. Even though Swift 2.3 and Swift 2.2 are similar Foundation API imported to those swift versions are rather different. This issue prevent using the same code base in Xcode 7 and Xcode 8 in the same time. – salabaha Jun 15 '16 at 15:10
  • 1
    @MartinR this is expected according to [This Swift.org Blog Post](https://developer.apple.com/swift/blog/?id=36). Quote *"Xcode project templates all use Swift 3"*. Thus, you have nothing to do but convert them manually to use Swift 2.3 – Yevhen Dubinin Jul 03 '16 at 19:46
  • 7
    The OP asked for `Swift 2.2`. Do you know if it's possible to use `Swift 2.2` in Xcode 8? – NSTJ Sep 05 '16 at 01:28
  • Furthermore if you depend on libraries that are not migrated to Swift 2.3 or Swift 3 you cannot use Xcode 8. Thats a big sh*** – blackjacx Sep 14 '16 at 08:14
  • 27
    Guys, the question asks for making Swift 2.2 code work in Xcode 8.0. Why would you explain it for Swift 2.3? – Murat Yasar Sep 17 '16 at 23:24
  • 2
    It is not about Swift 2.2. I downvote that answer. If you consider the same please flag that answer accordingly as I did. – Bartłomiej Semańczyk Oct 04 '16 at 21:45
  • Please answer for Swift 2.2. – K.K Dec 09 '16 at 08:19
  • Note that Xcode 8.3 doesn't support Swift 2.x http://stackoverflow.com/questions/43107179/xcode-8-3-cant-support-swift-2-3 – ThomasW May 15 '17 at 02:53
  • I'm on Xcode 8.2.1. I'm already using this setting for an old project. I just wanted to do some proof of concept in a **new** project. But when I open a new project, and change this to YES, still the syntax the autocomplete gives is Swift3. Did it just work for you straight out of the box? Or you need to do something extra? **EDIT:** I opened a new project but the first thing I did was to change the build settings, now it works. Before that I initially copied some code...and it was no longer working. Not sure if changing buildsetting first is a necessity or what...@MartinR – mfaani Oct 06 '17 at 14:32
33

Using Swift 2.3 in Xcode 8 by That Thing in Swift provides a good introduction to using Swift 2.3 in Xcode 8.

The following is quoted from the article:

There’s a single build setting that will let you continue building your Swift projects with a Swift version that’s mostly similar in syntax to your existing projects from Xcode 7: Use Legacy Swift Language Version

Just drop into your project’s build settings and search for legacy swift to find the correct build setting, then switch the setting to YES to opt-in to Swift 2.3 rather than Swift 3 in Xcode 8.

enter image description here

Babatunde Adeyemi
  • 14,360
  • 4
  • 34
  • 26
  • Mine already set to "YES", but I'm still getting "Use legacy swift version error". Strange enough, it isn't coming from the project. It's coming from UITest framework. – KMC Nov 24 '16 at 07:26
  • NVM, I just used cdm + alt +shft + k, and the error is gone. – KMC Nov 24 '16 at 07:32
  • 6
    In my Xcode 8.3.2, there is no such setting. Under `Swift Compiler`, there is just one build setting named `Swift Language Version` and `Swift 3` is the only choice. – thundertrick May 24 '17 at 02:59
  • I did same but still getting lots of errors can you pls share different way for solving it. – Chandni Oct 25 '17 at 11:24
6

It is not possible to use Swift2.2 in XCode8, and it is also not possible to use Swift2.3 or Swift3 in XCode3.

The best solution i found is to create a single project file that will compile for both iOS 9 (Xcode 7) and iOS 10 (Xcode 8), and that will support Swift2.2 and Swift2.3 (very similar).

yonivav
  • 994
  • 11
  • 18
3

As one of possible solutions to this problem I could suggest to install two versions of Xcode (7.3.1 and 8, for example) side by side.

To accomplish this you should:

  1. Download suitable dmg file from Downloads for Apple Developers. Use search to find Xcode.
  2. Double click this file to see familiar “Drag to install Xcode in your Applications folder” window. But drag this somewhere else for a moment. Desktop would be good. Rename it to Xcode \number of version\ (like Xcode 7.3.1) and then drag it to Applications folder.

That's it.

Artem Kirillov
  • 1,132
  • 10
  • 25
1

It is not possible to use Swift2.2 in XCode8 by setting Use Legacy Swift Language Version to Yes in Build Settings.

tania_S
  • 1,350
  • 14
  • 23