Apple's Swift programming language: The smart person's guide

banner-768x90

.com/blogger_img_proxy/

Image: Apple

Objective-C has won the hearts of many developers. It started as a programming language that bundled object-oriented programming (OOP) with the likeness of the C programming language. Objective-C called NeXT and Apple home where it was the default programming language for NeXTSTEP, OS X, and iOS.

In 2010, Apple started developing Swift, a new programming language that would rival Objective-C in a few areas — specifically, type safety, security, and better on-hardware performance. Swift is more than 2.6x faster than Objective-C and more than 8.4x faster than Python. Swift 1.0 was released in September 2014.

Executive summary

  • What is it? Swift is the newest programming language from Apple; it offers better type safety, security, and performance. Swift is available for developing iOS, OS X, watchOS, and tvOS apps.
  • Why does it matter? It's an easy programming language to learn, which is why many students, entry-level developers, and long-standing Mac and iOS developers are focusing their development skills on Swift. In addition, Apple open sourced Swift, making it available for developing on other platforms, not just those designed by Apple.
  • Who does this affect? Apps built with Swift can be run on iOS devices dating back to iOS 7 or later, and OS X devices dating back to OS X 10.9 or later.
  • When is it available? Swift is readily available in the most recent version of Xcode.
  • How can you get it? Get the latest version of Swift by downloading Xcode from the Mac App Store or the Apple Developer Center. Once Xcode is installed, Swift and the Objective-C compiler (LLVM) will be installed on your Mac.

SEE: Free ebook: Executive's guide to Apple in the enterprise

How Swift differs from Objective-C

What's Hot at TechRepublic

While Apple hasn't announced plans to sunset the Objective-C language (in fact, Apple is still updating it), Swift is rapidly overtaking Objective-C to become more popular on Apple's platforms.

Objective-C likely still has a long life, as Apple has yet to update its own Frameworks to be written in Swift. Until Swift 3.0, Apple will not be including the Swift runtime on iOS or OS X, leaving Xcode to package together the runtime into the iOS or OS X app to ensure binary compatibility.

Swift does bring a lot of modern programming niceties to Apple's platforms. Here's a look at the main differences between Objective-C and Swift.

Header files

Objective-C was based on C, but added OOP paradigms to the language through object messaging. Because of that, Objective-C made use of header files that publicly declared its functions and definitions.

Swift gets rid of this practice, allowing developers to write a single .swift file that contains typical header information, properties, and all of the class defines into a single file. Say goodbye to header files, and hello to cleaner and leaner code.

REPL

Objective-C and Swift are compiled languages, despite Swift having a REPL (Read-Eval-Print Loop) for testing that typically only supports interpreted languages.

The REPL is available in the command line and Xcode. Inside of Xcode, it is known as Playgrounds, allowing developers to write Swift code, and have it evaluated immediately, printing out the results in the sidebar (Figure A). To create a new Swift Playground in Xcode, open Xcode and select File | New | Playground. A new window will appear, and you can enter Swift code to test things.

Figure A

.com/blogger_img_proxy/

Playgrounds are a place to evaluate and test code without creating an entire iOS or OS X project first.

Image: Cory Bohon/TechRepublic

Type inference

With Objective-C and Swift, object types are strongly typed — meaning, the compiler must know exactly what type of object (string, array, dictionary, custom object, etc.) needs to be stored in memory for a particular variable. With Swift, Apple decided to keep strong types, but allow the compiler to automatically infer the type based on the objects assigned to a variable.

This means you can write code like the following snippet, and Swift will see that myVariable is a String type, because it has a String assigned to it.

let myVariable = "some string here"

Developers can still perform strong typing for clarity by doing this, but it is unnecessary. However, it does add clarity, especially to developers that may be touching the codebase after you've worked on it.

let myVariable: String = "some string here"

Available platforms

When Apple announced Swift at WWDC 2014, the company reported that conference attendees were using one of the first publicly released Swift apps: the WWDC app. It was developed partially in Swift, and allowed attendees to view session schedules, maps, and more.

Officially, iOS 7 and higher support Swift; OS X 10.9 (Mavericks) and higher support it as well. All versions of watchOS and tvOS have support for running Swift-built apps.

Additional resources:

How to learn and use Swift

You'll need a Mac, and if you will be developing for iOS, watchOS, or tvOS, you'll need one of those devices as well.

After you download and install Xcode, the Objective-C and Swift compiler (LLVM) is installed on your Mac. At this point, opening Xcode will present options for beginning projects in either Swift or Objective-C.

The first place to start is with Playgrounds and the Swift REPL, where you can learn by playing with Swift's features in an environment that won't mess up any in-flight apps, and without the need to create any full testing iOS or OS X project.

Be sure to take advantage of Apple's great Swift resources, which include two books: one on getting started and one on integrating Swift with existing Objective-C apps.

Additional resources:

What Swift means for enterprises and developers

Swift is an open source language that is being adopted at a very rapid pace. It allows developers to prototype and write iOS, OS X, and other Apple platform apps faster and with fewer bugs and crashes than ever before.

This is significant for enterprises, especially businesses that have made investments in Apple's platforms, and investments in developing their own B2B/enterprise apps, because keeping those apps up to date has never been easier.

In addition, Swift being open sourced means that the language will be expanded to other platforms, beyond just what Apple has created. We are already seeing Swift being ported to Linux and other platforms.

One potential area where Swift will flourish is web apps. Swift being ported to Linux means that Swift apps can now run on low-cost, low-maintenance Linux servers that are already the cornerstone of existing web APIs and services. There are already frameworks for Swift that make it so Linux-based server apps can be built inside of Xcode, allowing businesses to utilize existing Swift developers to build the APIs and services that iOS and OS X apps often consume.

Additional resources:

What's in Swift 2.0

Apple released Swift 2.0 at WWDC '15, and it added many features and refinements. These are some of the new features in Swift 2.0.

  • Swift is open source with a new Linux port available.
  • A new error handling model using try, throw, and catch keywords.
  • It targets older versions of iOS and OS X. It's also safer, with the #available block that lets you wrap lines of code that will be executed on systems where the framework is available.
  • SDKs are now Swift-ier, thanks to Apple adding generics and nullability to existing Objective-C frameworks to make them interface better with Swift code.

We'll likely hear about Swift 3.0 at WWDC '16. The Swift 3.0 roadmap on GitHub includes a list of features that may be included with that release.

Additional resources:

Version history

  • Mid 2010: Development begins
  • July 17, 2010: First Swift commit to the Swift GitHub repository
  • June 2, 2014: Apple announces Swift at WWDC 2014, giving developers a pre-release version of Swift and Xcode 6.
  • September 15, 2014: Apple releases Swift 1.0 with the Gold Master of Xcode 6.
  • October 15, 2014: Swift 1.1 is released with Xcode 6.1.
  • April 8, 2015: Swift 1.2 is released with Xcode 6.3
  • June 8, 2015: Apple announces Swift 2.0 at WWDC 2015, giving developers a pre-release version of Swift 2 and Xcode 7.
  • September 15, 2015: Apple releases Swift 2.0 with the Xcode 7 Gold Master build.
  • October 20, 2015: Apple releases Swift 2.1 with the release of Xcode 7.1.
  • December 3, 2015: Apple announces the Swift 3.0 roadmap on GitHub.
  • March 21, 2016: Apple releases Swift 2.2 with the release of Xcode 7.3.

Additional resources:


Source: http://techrepublic.com.feedsportal.com/c/35463/f/670841/s/4ebe4964/sc/28/l/0L0Stechrepublic0N0Carticle0Capples0Eswift0Eprogramming0Elanguage0Ethe0Esmart0Epersons0Eguide0C0Tftag0FRSS56d97e7/story01.htm
Apple's Swift programming language: The smart person's guide Reviewed by Unknown on 4/05/2016 Rating: 5

Post Comments

Powered by Blogger.