How to write apps with Swift 3

All you need to know about writing apps with Apple's Swift 3 developer language: new features in Swift 3, and why you should you learn to program in Swift

Swift is used to write or create apps for macOS and iOS devices. Apple designed Swift explicitly to get the fastest and most efficient performance from devices, and Swift 3 expands upon its already impressive feature set.

What's new in Swift 4?

"Swift was released as an open-source project just six months ago," says Ted Kremenek, Senior Manager, Languages and Runtimes at Apple. "A big part of doing that was not just to shove out a bunch of sources to GitHub, but to create a fully open community that drives the evolution of swift going forward. Swift 3 is the first major update to swift that is intended to a product of that community."

Another big aspect for Swift is that it's no longer just about iOS and OS X development. Since Swift went open-source, a version has appeared for Linux computers. This has opened up Swift to server developers, as well as app coders.

"Today's software ecosystems are really diverse," says Kremenek. Whether they're working on servers, or working on apps. We want Swift to be for everybody. This is the first major update since Swift was ported to Linux."

How to write apps with Swift 3: Getting the core fundamentals right with new Swift 3 API language

What may surprise developers who've been using Swift 2 (or before) is how much the language is changing in Swift 3. "A big thing about Swift 3 is that we really want to get those core fundamentals into shape and build on top of them going forward," says Kremenek. "We really want to awesomize Swift for awesomeness."

"This is a hard problem. This isn't just a matter of if we use commas or colons. It's also about compatibility. So we're doing everything we can to get Swift into that shape so we can live with it forever."

So there are bunch of new features in Swift:

Accessing APIs in Swift 3. The biggest change is to the API language. Accessing Apple APIs is an essential part of building software in Swift (and most modern languages). Apple has radically changed the API language to emphasize clarity. You can read more about the new syntax at Swift.org.

Playground support. There is now Playground support for downloadable snapshots in Xcode 8. This means you can download the latest snapshot of Swift in Xcode and start experimenting right away. You don't even need to restart Xcode. This should make it a lot easier for developers to keep on top of the new language as it evolves.

Parameter labelling. There are some quite heavy changes to parameter labelling in Swift 3. In Swift 2 it was consistent with Objective-C, but because the API language has changed so much, it makes sense to make parameters consistent with Swift.

Generics are another area that's seen a change of syntax. The signature is now up front, and constraints are secondary to that.

Warnings. If you have unused results in a function, you now get a warning. You can override this warning if the behaviour is intentional.

As well as adding features to Swift 3.0, Apple is taking features away. Here are some removed features for Swift 3.0.

Currying func declaration syntax

var in function parameter lists

++ and -- operators

C-style for loop

Implicit tuple splat in calls

"Some of these, I realise, may be polarising or controversial," says Kremenek. But he points out that they are the result of extended discussion with the developer community, and all the logs are available online.

How to write apps with Swift 3: Install Xcode 8 beta

Swift 3.0 is now available to all developers, and you don't need to be part of the paid-for Apple Developer Connection to start using it. Downloading the Xcode 8 beta enables you to start using the Swift 3.0 language right away. Apple has also made the Swift 3 Programming Language guide available on iBooks Store, so you can get up and running with the new features and syntax.

How to write apps with Swift 3: What does Swift being open-source mean?

As of December 2015, Swift 2 was open source. But what does that mean for programmers and app users?

Open-source typically means that the source code behind a program, or programming language, is made available to the general public. Coders can then inspect, modify and deploy the program wherever they want.

In practice this means that developers can take the Swift programming language and build it for deployment on non-Apple operating systems. In particular we now see Swift on Linux OSes (because both Linux and OS X share a similar underlying Unix-like structure).

Theoretically you could also see Swift implemented on Windows computers eventually, although we are still waiting to see a usable version of Swift on Windows. There are also rumours that Google, which currently uses Java for Android, could move to Swift. We think this is unlikely, especially after Google won its API in Java copyright case against Oracle.

Great! Will we be able to run iOS apps on Android phones?

While you'll be able to use Swift code on other devices, don't believe that this means you'll be seeing OS X and iOS software running on Windows and Android. While it'll make it easier to port software from one device to another, we expect Apple to retain control of its SDKs (Software Development Kit). While we don't know which parts of Swift Apple intends to make open-source, we believe Apple will still retain the source code for Xcode, so you'll still be using a Mac to develop OS X and iOS software.

While Apple is a very private company, and very protective of its own creations, so to a non-developer open source seems an odd fit for the company. However, Apple isn't a stranger to the open-source community by any means. OS X is built upon UNIX and much of its software components are open source.

Apple's Open at the source page says: "Apple believes that using Open Source methodology makes Mac OS X a more robust, secure operating system, as its core components have been subjected to the crucible of peer review for decades."

How to write apps with Swift 3: What is Apple's Swift programming language like to use?

Apple's Swift programming language, first unveiled at WWDC 2014, is designed to make it much easier to program Mac OS X and iOS devices. The rest of this article looks at what Apple Swift is like as a programming language, how to get Apple Swift set up on your Mac, and how to learn to program in Apple Swift.

Swift augments, and is set to eventually replace, Apple’s current Objective-C programming language. Objective-C was developed in the 1980s and brought to the Mac in 1996. Prior to 2014 it had been 17 years since Apple released a new programming language so Swift is very important to the Apple developer community.

How to write apps with Swift 3: What is Apple's Swift programming language?

Swift is a programming language for Cocoa and Cocoa Touch (which are the programming frameworks for Mac OS X and iOS, respectively). Swift programs are created using Xcode 6, the latest edition of Apple’s integrated development environment (IDE).

Swift is a relatively new programming language, introduced in 2014, but it is designed to sit alongside existing Objective-C programs (which is what OS X and iOS programs used to be developed with This enables developers to add Swift code to existing apps without having to replace all the older Objective-C code.

How to write apps with Swift 3: What are the advantages to programming in Apple Swift?

Apple has outlined a number of advantages that Swift has over older Objective-C code. Since the language is relatively new there is much that remains to be see, but here are some of the reasons why Swift is a better programming language than Objective-C:

Apple Swift has clean syntax

Apple Swift is designed to be a much easier programming language to read, and code. Swift's syntax (the formatting) does not require semi-colons at the end of each line, and functions are easier to understand. For example, the humble Print command, which most people are familiar with from Basic is NSLOG in Objective-C (NS is a throwback to NextStep, the company that Steve Jobs set up when he left Apple, which shows you how much legacy Apple is replacing); in Swift that command is a far more familiar 'println'. There are fewer symbols required to code, and in general Swift is much more efficient than Objective-C.

If we look at the classic Hello World program in each language (the program that writes “Hello, world!” on the screen). You can see that Swift is simpler, and makes much more sense than Objective-C.

Objective-C: Hello World

Here is how the Hello World program appears in Objective-C:

#import

#import <Foundation/Foundation.h>

int main(void)

{

NSLog(@"Hello, world!\n");

return 0;

}

Swift: Hello World

Here is how the Hello World program looks in Swift:

println("Hello, world!")

As you can see Swift is a much cleaner and simpler code to read and learn. Here are some other features that Apple lists as important to ensuring code is 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, protocols.

Functional programming patterns, e.g.: map and filter

Apple Swift's memory is managed

One of the big advantages to Swift is that developers do not have to manage memory allocations. In Swift variables are initialized before use, arrays and integers are checked for overflow and memory is managed automatically. This makes the Swift programming language safer to use for developers who aren’t quite as experienced. Swift’s memory management will make apps more reliable, which will benefit developers and non-developers.

How to write apps with Swift 3: Where can I get Apple Swift for my Mac?

Swift is part of the Xcode IDE (integrated development environment) available as a free download from the Mac App Store.

How to write apps with Swift 3: Should I learn how to program in Swift?

The general consensus from developers appears to be that Swift is a great programming language. Swift is similar in many ways to Python, another highly-regarded programming language. Swift should be a much easier programming language for newcomers to pick up than Objective-C. Python is regularly used as the language of choice for programming classes, so many programmers may already be familiar with much of the Apple Swift syntax.

But it's also better for established developers who will find the cleaner syntax easier to use.

If you've spent time becoming familiar with Objective-C you should find much about Swift that makes life easier, although having to move to a new programming language requires learning new code, which will be a distraction for current developers. But in the long run Swift coding is expected to pay off even for seasoned Objective-C developers.

Swift is not going to make it easy to move code from Mac OS X and iOS to other platforms, such as Android. Many developers are weighing up the advantages of Swift development against further lock-in to the Apple environment. For the time being Apple is supporting both Objective-C and Swift code for building Cocoa and Cocoa Touch apps, but eventually we expect it to move everybody over to Swift.

How to write apps with Swift 3: How do I learn Swift programming?

If learning Swift takes your fancy then there are a range of resources available. You can pick up Xcode from the App Store and Apple has already released a book on Swift programming on the iBooks Store: The Swift Programming Language (iBooks Store). Anybody can download the book incidentally, you don't need to be a registered Apple Developer.

Apple's Swift Programming Language book can be read using iBooks on Mac OS X, or you can read it on an iPad (or iPhone). Apple’s book takes you from Basic Operators through to Inheritance; but The Swift Programming Language book does not cover iOS app development in the Xcode environment. We expect more detailed books on Swift, as well as tutorials for beginners, to appear shortly.

Please let us know of any more Swift programming websites (or books) in the comments section.

How to write apps with Swift 3: What's the best way to learn Swift programming from scratch?

If you're a newcomer to programming, or want to learn how to program in general then there are a range of websites and services that can help. Here are some of the best coding sites to look at:

Codeacademy. This free online learning community teaches digital skills. It doesn't feature Swift programming, yet, but it does hold your hand while you learn Python (which is a great langauge to start with).

Learn Python the Hard Way. Despite the rather sinister sounding name this book and course is the best way we know for beginners to pick up programming.

MITX 6.00.1x: Introduction to Computer Science and Programming Using Python. This course is designed for MIT and Harvard students with no prior experience of computer science or programming. And all the materials and courses are available online. It's the best rough guide to getting started with programming we know of (the videos are pretty neat to watch if you're just interested in computing in general).

If that's not enough you can already play Flappy Bird in Swift thanks to a developer called Nate Murray. The Swift code for Flappy Bird is available over on GitHub.

Let us know of any other links or resources you think we should include.