Swift 4

The powerful programming language that is also easy to learn.

Swift is a powerful and intuitive programming language for macOS, iOS, watchOS and tvOS. Writing Swift code is interactive and fun, the syntax is concise yet expressive, and Swift includes modern features developers love. Swift code is safe by design, yet also produces software that runs lightning-fast.

Introducing Swift 4

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 smart key paths and serialization, all while shortening build times and reducing the size of app binaries.

Learn Swift on iPad

In Swift Playgrounds you create small programs called “playgrounds” that instantly show the results of the code that you write. A single line of code can make amazing things happen. Interactive lessons teach key coding concepts, and additional challenges and templates encourage you to explore code in exciting new ways and to create something completely unique. There are even playgrounds that use Bluetooth to control robots, drones, and other hardware accessories. It’s easy to share your creations with friends, or record and post videos of your playgrounds in action.

Learn about Swift Playgrounds for iPad

Open Source

Swift 4 was developed in the open at Swift.org, with source code, a bug tracker, mailing lists, and regular development builds available for everyone. This broad community of developers, both inside Apple as well as hundreds of outside contributors, work together to make Swift even more amazing. Swift already supports all Apple platforms as well as Linux, with community members actively working to port to even more platforms. We're excited to see more ways in which Swift makes software safer and faster, while also making programming more fun.

To learn more about the open source Swift community, visit Swift.org

Source Compatibility Modes

Swift 3 set up the language for source-level stability and now Swift 4 delivers source compatibility as a feature moving forward. With Swift 4, you don’t have to modify any of your code to use the new version of the compiler. Instead you can start using the new Swift 4 compiler and migrate at your own pace, taking advantage of new Swift 4 features, one module at a time.

You can use the new Swift 4 compiler with three different modes:

  1. Swift 3 mode is the default for existing code and will build source code that built with the Swift 3 compiler
  2. Swift 4 mode enables you to use new features and performance optimizations of Swift 4, some of which may require migration, making it easier to migrate than the previous transition from Swift 2.2 to Swift 3.
  3. Mixed mode allows you to take advantage of interoperability between binaries built with the same compiler. This helps developers who have projects that mix packages written with Swift 3 with packages written with Swift 4, as long as all packages are built using the Swift 4 compiler. This allows developers to gradually migrate specific portions of their code to Swift 4 over time.

New Features in Swift 4

  • Faster, easier to use Strings that retain Unicode correctness and add support for creating, using and managing substrings
  • Smart key paths for type-safe, efficient, extensible key value coding for Swift types
  • Enhancements to creating and manipulating Dictionary and Set types
  • Extends support of archival and serialization to struct and enum types and enables type-safety for serializing to external formats such as JSON and plist
  • Enforced exclusive access to memory

Modern

Swift is the result of the latest research on programming languages, combined with decades of experience building Apple platforms. Named parameters brought forward from Objective-C are expressed in a clean syntax that makes APIs in Swift even easier to read and maintain. Inferred types make code cleaner and less prone to mistakes, while modules eliminate headers and provide namespaces. Memory is managed automatically, and you don’t even need to type semi-colons. These forward-thinking concepts result in a language that is easy and fun to use.

extension String {
	var banana : String {
		let shortName = self.dropFirst()
		return "\(self) \(self) Bo B\(shortName) Banana Fana Fo F\(shortName)"
	}
}

let bananaName = "Jimmy".banana		// "Jimmy Jimmy Bo Bimmy Banana Fana Fo Fimmy"

Swift has many other features to make your code more expressive:

  • Closures unified with function pointers
  • Tuples and multiple return values
  • Generics
  • Fast and concise iteration over a range or collection
  • Structs that support methods, extensions, and protocols
  • Functional programming patterns, e.g., map and filter
  • Native error handling using try / catch / throw

Playgrounds and REPL in Xcode

Much like Swift Playgrounds for iPad, playgrounds in Xcode make writing Swift code incredibly simple and fun. Type a line of code and the result appears immediately. You can then Quick Look the result from the side of your code, or pin that result directly below. The result view can display graphics, lists of results, or graphs of a value over time. You can open the Timeline Assistant to watch a complex view evolve and animate, great for experimenting with new UI code, or to play an animated SpriteKit scene as you code it. When you’ve perfected your code in the playground, simply move that code into your project.

Read-Eval-Print-Loop (REPL)

The LLDB debugging console in Xcode includes an interactive version of the Swift language built right in. Use Swift syntax to evaluate and interact with your running app, or write new code to see how it works in a script-like environment. Available from within the Xcode console or in Terminal.

Package Manager

Introduced in Swift 3, Swift Package Manager is a cross-platform tool for building Swift libraries and executables, making it easy to create and manage dependencies. Swift 4 adds 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, to work on a branch of multiple packages together, and to make local changes to the source code of a dependency you don't control. New API lets package authors define which products their packages make available to clients, and customize how their sources are organized on disk. And the overall API used to create a package is now cleaner and clearer, while retaining source-compatibility with older packages.

Fast and Powerful

From its earliest conception, Swift was built to be fast. Using the incredibly high-performance LLVM compiler, Swift code is transformed into optimized native code that gets the most out of modern hardware. The syntax and standard library have also been tuned to make the most obvious way to write your code also perform the best.

Swift is a successor to both the C and Objective-C languages. It includes low-level primitives such as types, flow control, and operators. It also provides object-oriented features such as classes, protocols, and generics, giving Cocoa and Cocoa Touch developers the performance and power they demand.

Designed for Safety

Swift eliminates entire classes of unsafe code. Variables are always initialized before use, arrays and integers are checked for overflow, and memory is managed automatically. Syntax is tuned to make it easy to define your intent — for example, simple three-character keywords define a variable ( var ) or constant ( let ).

Another safety feature is that by default Swift objects can never be nil. In fact, the Swift compiler will stop you from trying to make or use a nil object with a compile-time error. This makes writing code much cleaner and safer, and prevents a huge category of runtime crashes in your apps. However, there are cases where nil is valid and appropriate. For these situations Swift has an innovative feature known as optionals. An optional may contain nil, but Swift syntax forces you to safely deal with it using the ? syntax to indicate to the compiler you understand the behavior and will handle it safely.

Objective-C Interoperability

You can create an entirely new application with Swift today, or begin using Swift code to implement new features and functionality in your app. Swift code co-exists along side your existing Objective-C files in the same project, with full access to your Objective-C API, making it easy to adopt.

To get started with Swift, download Xcode and follow the tutorials available on the Resources tab.