During the WWDC2014 keynote, Apple made the
exciting announcement that a brand new programming language was nearing
release. Within 24 hours, 370,000
developers had downloaded Apple's new book entitled "The Swift Programming
Language" from the iBooks Store. In
this post, we'll introduce Swift and it's major language features at a high
level. Future posts will delve deeper
into specific topics.
Swift is intended to be a simpler, more
elegant, safer, and more modern programming language compared to it's
predecessor. It is expected that Swift will gradually replace Objective-C as
the de facto programming language for iOS and OS X applications. In order to
support a gradual transition, Swift is fully interoperable with
Objective-C. You can easily write
programs that consist of a blend of both Swift and Objective-C code. There are
a few caveats, such as the fact that you cannot currently subclass a Swift
class in Objective-C. These caveats may come into play if you plan on doing a gradual
migration of your code from Objective-C to Swift.
Swift supports every core data type that you
would expect: signed and unsigned
integers in standard sizes (8,16,32,64), floating points, strings, arrays,
structs, enums, classes, interfaces, and even generics. Additional interesting concepts built into the
language include tuples, optionals, and closures.
Swift constants and variables are declared
with the let and var keywords, respectively.
Swift is strongly typed, although Apple believes that explicit type
annotation is not necessary in many cases, due to Swift's sophisticated type
inferencing capabilities. In the simple
example below, the myInt constant will be strictly enforced as an Int type
throughout the program. The variable myDouble will be strictly treated as a
Double. More specific types are created with
type annotations using a colon, as shown in the myShort and myFloat examples
below.
let myInt = 10 //
inferred and enforced as Int
var myDouble = 10.1 // inferred and enforced as Double
let myShort: UInt8 = 10 // Smaller range
var myFloat: Float = 10.1 // Less precision
Although it is not real-world applicable, the
Swift team enjoys pointing out that it only takes 1 line of code to have a
complete, stand-alone, executable program.
println("Welcome to Swift")
A main() or equivalent entry point is not
strictly necessary, since execution will begin with code defined at the global
scope. One simplification that I can't
seem to train my brain to accept is that semi-colons are not required, unless
you are placing multiple statements on a single line. Finally, it is not
necessary to explicitly include external libraries for basic functionality such
as printing output to the console.
These are "tip of the iceberg"
examples of how Swift better represents Apple's overall mantra of making things
simple and intuitive. Memory management also
occurs automatically in Swift, eliminating the need to track allocations and
releases.
Version 6 of Apples Xcode IDE includes full
support for Swift. There is even a slick new feature called Playgrounds, which
allows "interactive programming" where you see instant results of
your code as you are developing it, without requiring you to compile and run a
complete project.
It's important to remember that Swift is
barely beginning its life cycle as a programming language. New OS X and iOS developers will still need
to learn and understand Objective-C in order to be truly effective. That said, Swift represents an important step
for Apple in increasing adoption among the software development community.