Keywords: Xcode 9 | Swift Language Version | SWIFT_VERSION Error | Code Migration | Swift 3.2
Abstract: This article delves into the Swift Language Version (SWIFT_VERSION) setting error encountered in Xcode 9. It begins by analyzing the root cause: Xcode 9 only supports migration from Swift 3.0 to Swift 3.2 or higher, and projects with versions below Swift 3.0 require conversion via Xcode 8.x first. Two main solutions are detailed: installing and using Xcode 8.x for code migration, including downloading older versions, configuring command-line tools, and step-by-step migration procedures; and directly setting SWIFT_VERSION to 3.2 in Xcode 9, particularly useful for Objective-C projects. Best practices for code migration, such as using Xcode's "Convert to Current Swift Syntax" feature, are provided, with emphasis on the compatibility of Swift 3.2 across Xcode 8 and 9. Through systematic analysis and guided steps, this article aims to help developers efficiently resolve version compatibility issues and ensure smooth project upgrades.
Problem Background and Error Analysis
After upgrading to Xcode 9, many developers encounter a common build error: "The ‘Swift Language Version’ (SWIFT_VERSION) build setting must be set to a supported value for targets which use Swift. This setting can be set in the build settings editor." This error often occurs in projects using CocoaPods for dependency management, where executing pod update reinstalls some Pods (e.g., Alamofire), but Xcode 9 cannot automatically handle code migration for versions below Swift 3.0. The core issue is that Xcode 9's migration mechanism only supports conversion from Swift 3.0 to Swift 3.2 or 4.0; if the project's Swift version is lower than 3.0, it must first be upgraded using Xcode 8.x.
Solution 1: Code Migration Using Xcode 8.x
For projects with Swift versions below 3.0, the most effective solution is to install Xcode 8.x for initial migration. Developers can download Xcode 8.3.3 or earlier from the Apple Developer Portal (e.g., https://developer.apple.com/download/). Both Xcode 9 and Xcode 8.x can be installed on the same macOS system, but ensure command-line tools support both versions. In the terminal, switch the active version using xcode-select, e.g., sudo xcode-select -s /Applications/Xcode8.app/Contents/Developer. For migration, open the project in Xcode 8 and use the "Edit ▶ Convert ▶ To Current Swift Syntax" feature to upgrade the code to Swift 3.2. Once done, the project can be continued in Xcode 9 with Swift 3.2 or 4.0 as the language version.
Solution 2: Directly Setting SWIFT_VERSION in Xcode 9
For Objective-C projects or partially migrated Swift projects, SWIFT_VERSION can be manually set in Xcode 9. In project settings, navigate to "Build Settings", search for "Swift Compiler Language", find the "Swift Language Version" option, and set it to 3.2. If the option is missing, click the "+" button to add a user-defined setting with key SWIFT_VERSION and value 3.2. This method is particularly useful for pure Objective-C projects, as Xcode 9 might incorrectly detect Swift dependencies and throw errors. After setting, rebuild the project to resolve the error.
Best Practices for Migration and Compatibility Notes
When migrating, follow these steps for a smooth transition: First, complete all code conversion to Swift 3.2 in Xcode 8, leveraging Xcode's automatic migration tools to minimize manual errors. Second, after opening the project in Xcode 9, immediately check the Podfile and dependencies to ensure all Pods support Swift 3.2 or higher. Swift 3.2 is a critical version, supported in both Xcode 8 and Xcode 9, serving as a compatibility bridge. Note that Xcode 9 does not support direct migration from Swift 2.x to Swift 4.0, making incremental upgrades necessary. Additionally, regularly back up projects and run tests before migration to prevent data loss and functional regressions.
Code Examples and In-Depth Analysis
To better understand the migration process, consider this Swift 2.0 code snippet that triggers the SWIFT_VERSION error in Xcode 9: func oldSyntax() -> String { return "Hello" }. In Xcode 8, after using the migration tool, the code might be converted to Swift 3.2: func newSyntax() -> String { return "Hello" } (note that Swift 3.2 syntax is similar to Swift 3.0 but includes Xcode 9 optimizations). If SWIFT_VERSION is set in Xcode 9 without migration, old code may fail to compile due to stricter compiler requirements for Swift 3.2. For example, common NSURL usage in Swift 2.0 needs to change to URL in Swift 3.2, highlighting the importance of migration. By combining automatic tools and manual adjustments, developers can efficiently resolve version conflicts.
Conclusion and Recommendations
In summary, the SWIFT_VERSION error in Xcode 9 stems from version compatibility issues, with main solutions including migration via Xcode 8.x or manual setting in Xcode 9. For most Swift projects, it is recommended to first upgrade to Swift 3.2 using Xcode 8, then leverage Xcode 9's new features. Developers should monitor Apple's update announcements, as future Xcode versions may alter migration strategies. Keeping project dependencies updated and conducting regular testing are key to preventing such issues. With this guide, developers can smoothly transition to Xcode 9 and enjoy the latest improvements in Swift.