Apple revealed a brand new programming language, called Swift, atWWDC 2014. Swift is designed to make it much easier to program Mac OS X and iOS devices. This article looks at what Apple Swift is like as a programming language, how to get Apple Swift setup on your Mac, and how to learn to program in Apple Swift.
Apple Swift is a big deal. 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. It has been 17 years since Apple released a new programming language so Swift is very important to the Apple developer community.
- Apple unveils Swift, a new programming language for iOS and Mac OS X
- At developer conference, Apple releases slew of new developer tools
- What did Apple launch at WWDC 2014?
What is Apple’s Swift programming language?
Swift is a new 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 new programming language, but it is designed to sit alongside existing Objective-C programs (which is what all current Mac OS X and iOS programs are developed in). This enables developers to add Swift code to existing apps without having to replace all the older Objective-C code.
What are the advantages to 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. It’s syntax does not require semi-colons at the end of each line, and there is far less clutter to code.
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
#import
#import <Foundation/Foundation.h>
int main(void)
{
NSLog(@"Hello, world!\n");
return 0;
}
Swift: Hello World
println("Hello, world!")
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
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.
Where can I get Apple Swift for my Mac?
Swift is part of the Xcode-6 IDE (integrated development environment). We expect Xcode-6 to be released on the App Store (for free) alongside Mac OS X 10.10 Yosemite in the Autumn.
In the meantime Apple developers can get hold of the Xcode-6 beta from the Apple Developer connection. You need to be a registered iOS or Mac OS X developer, however, which costs £69 per year.
See also
- OS X Yosemite unveiled at WWDC 2014, coming this autumn
- 10 for 10: 10 things we'd like to see in OS X 10.10
- Mac OS X 10.10 Yosemite release date, rumours & images
Should I learn how to program in Swift?
The general consensus is that Swift is a great programming language to learn. Swift is similar in many ways to Python, another popular 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.
If you’ve spent time become 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 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 over to Swift completely.
How do I learn Swift programming?
If learning Swift takes your fancy then there are a range of resources available. You can pick up the Xcode 6 beta from the Apple Developer connection, and Apple has already released a book on Swift programming on the iBooks Store:The Swift Programming Language (iBooks Store)
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.
Here are some Apple Swift programming resources:
- The Swift Programming Language (iBooks Store)
- Xcode 6 beta
- The Swift Programming Language
- Using Swift with Cocoa and Objective-C
Further to that there are plenty of online resources springing up to help people get started with Apple’s Swift programming. Here are some worth online sites Swift programmers can check out:
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 here on GitHub.
0 comments: