Swift 4 is now officially released! Swift 4 builds on the strengths of Swift 3, delivering greater robustness and stability, providing source code compatibility with Swift 3, making improvements to the standard library, and adding features like archival and serialization.
You can watch a quick overview of it by watching the WWDC 2017: What’s New in Swift presentation, and try out some of the new features in this playground put together by Ole Begemann.
Swift 4.0 is a major language release and contains the following language changes and updates that went through the Swift Evolution process:
Swift 4 includes a faster, easier to use
String implementation that retains Unicode correctness and adds support for creating, using and managing substrings.
See more at:
- SE-0163 String Revision: Collection Conformance, C Interop, Transcoding
- SE-0168 Multi-Line String Literals
- SE-0178 Add unicodeScalars property to Character
- SE-0180 String Index Overhaul
- SE-0182 String Newline Escaping
- SE-0183 Substring performance affordances
Swift 4 adds improvements for creating, using and managing Collection types.
See more at:
- SE-0148 Generic Subscripts
- SE-0154 Provide Custom Collections for Dictionary Keys and Values
- SE-0165 Dictionary & Set Enhancements
- SE-0172 One-sided Ranges
- SE-0173 Add
Archival and serialization
Swift 4 supports archival of struct and enum types and enables type-safe serialization to external formats such as JSON and plist.
See more at: SE-0166 Swift Archival & Serialization
Additional language updates
Swift 4 also implements the following language proposals from the Swift Evolution process:
- SE-0104 Protocol-oriented integers
- SE-0142 Permit where clauses to constrain associated types
- SE-0156 Class and Subtype existentials
- SE-0160 Limiting @objc inference
- SE-0164 Remove final support in protocol extensions
- SE-0169 Improve Interaction Between private Declarations and Extensions
- SE-0170 NSNumber bridging and Numeric types
- SE-0171 Reduce with inout
- SE-0176 Enforce Exclusive Access to Memory
- SE-0179 Swift run Command
New compatibility modes
With Swift 4, you may not need to modify your code to use the new version of the compiler. The compiler supports two language modes:
Swift 3.2: In this mode, the compiler will accept the majority of sources that built with the Swift 3.x compilers. Updates to previously existing APIs (either those that are part of the standard library or APIs shipped by Apple) will not appear in this mode, in order to provide this level of source compatibility. Most new language features in Swift 4 are available in this language mode.
Swift 4.0: This mode includes all Swift 4.0 language and API changes. Some source migration will be needed for many projects, although the number of source changes are quite modest compared to many previous major changes between Swift releases.
The language mode is specified to the compiler by the
-swift-version flag, which is automatically handled by the Swift Package Manager and Xcode.
One advantage of these language modes is that you can start using the new Swift 4 compiler and migrate fully to Swift 4 at your own pace, taking advantage of new Swift 4 features, one module at a time.
For more information about Swift 4 migration and compatibility modes, see Migrating to Swift 4
Package Manager Updates
Swift 4 introduces new workflow features and a more complete API for the Swift Package Manager:
- It’s now easier to develop multiple packages in tandem before tagging your first official release, or to work on a branch of multiple packages together.
- Package products have been formalized, making it possible to control what libraries a package publishes to clients.
- The new Package API allows packages to specify a number of new settings, giving package authors more control over how packages build or how sources are organized on disk. Overall, the API used to create a package is now cleaner and clearer, while retaining source-compatibility with older packages.
- On macOS, Swift package builds now occur in a sandbox which prevents network access and file system modification, to help mitigate the effect of maliciously crafted manifests.
Further, the Swift Package Manager builds on top of package manager tools versioning introduced in Swift 3.1 (SE-0159) which allows a package author to specify the version of Swift required for building a package — which now includes Swift 4.
For more information about enhancements to the Package Manager, see:
- SE-0146 Package Manager Product Definitions
- SE-0149 Package Manager Support for Top of Tree development
- SE-0150 Package Manager Support for branches
- SE-0158 Package Manager Manifest API Redesign
- SE-0162 Package Manager Custom Target Layouts
- SE-0175 Package Manager Revised Dependency Resolution
- SE-0179 Swift run Command
- SE-0181 Package Manager C/C++ Language Standard Support
An updated version of The Swift Programming Language for Swift 4.0 is now available on Swift.org. It is also available for free on Apple’s iBooks store.
Official binaries for Ubuntu 16.10, Ubuntu 16.04 and Ubuntu 14.04 are available for download.
For development on Apple’s platforms, Swift 4.0 ships as part of Xcode 9.
A toolchain is also available for download from Swift.org.
Development on Swift 4.0 was tracked in the swift-4.0-branch on the following repositories on GitHub:
swift-4.0-RELEASE designates the specific revisions in those repositories that make up the final version of Swift 4.0.
swift-4.0-branch will remain open, but under the same release management process, to accumulate changes for a potential future bug-fix “dot” release.