The Swift Programming Language

Swift is a programming language developed internally at Apple and released to the public in 2014 for use in developing iOS and OS X apps. In 2015, Apple made major updates to the language, upping its version number to 2.0. This is the version of the language supported by Xcode 7, and the version we’ll be using throughout this book. Apple is also open-sourcing the language, and, while it’s a work in progress, this may eventually lead to opportunities for Swift developers beyond the Apple ecosystem.

Swift is defined by several essential traits. Swift is:

  • Compiled, meaning that our source code is converted into bytecode at build time, rather than being interpreted at runtime, as with scripting languages like JavaScript and Ruby.

  • Strongly Typed, meaning that objects we work with are clearly identified as strings, integers, floats, and so on. These types are static, meaning they can’t change once they’re assigned. Swift also provides type inference, so we don’t have to explicitly indicate a type when the compiler can unambiguously figure it out.

  • Automatically Reference Counted, meaning that objects allocated from memory keep track of how many incoming references they have, and their memory is freed up once there are zero references to them (since they’re useless at this point, as no other objects now know about them). This is subtly different from the garbage collection system popular in many languages. The difference is that a garbage collector periodically goes out looking for unreferenced objects; Swift’s Automatic Reference Counting (ARC) keeps constant track of references and frees objects immediately when they have zero references.

  • Name-spaced, like many modern languages, but, importantly, not like Swift’s predecessor, Objective-C. Name-spacing allows us to avoid collisions when multiple parts of our code (our app, frameworks we import, etc.) use the same name for different things. We can safely use a generic name like MyThing, because it’s understood to be part of a module with a unique name like com.mycompany.myapp, so it won’t be confused with com.othercompany.otherapp’s MyThing (although we should still come up with better names than “MyThing”!)

Perhaps most importantly, Swift is a deeply pragmatic language, with a unique mission: providing a more modern and expressive way to code than Objective-C offered, while retaining compatibility with over 20 years of existing Apple frameworks and system code. The iOS SDK is written mostly in Objective-C, but parts of it are in C or even C++, and many different idioms are used in the various frameworks and libraries. Swift has to make it easy and natural to call all of it. As we’ll see, Swift makes some accommodations to the past, because a more linguistically clean alternative that would require rewriting the iOS frameworks would be a non-starter.

In this book, we are covering Swift 2.0, which is supported by Xcode 7. Earlier versions of the language were supported by Xcode 6. Swift and Xcode versioning moves in lock-step, meaning you can’t write Swift 1.2 syntax in Xcode 7, nor can you write Swift 2.0 in any version of Xcode 6. Fortunately, the compiled code is forward- and backward-compatible, so Swift 1.2 code written in Xcode 6 will run on iOS 9, and Swift 2.0 code written in Xcode 7 will run on iOS 8 (provided that the app as a whole is marked as being backward-compatible with iOS 8). For simplicity, we are focusing only on current versions in this book: Swift 2.0, Xcode 7, and iOS 9.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.189.22.55