On Monday Apple revealed iOS 8 and OS X 10.10 Yosemite. Both of these were expected by everyone who has followed Apple’s pattern of software reveals at WWDC for the past few years. But Apple also revealed new power tools for developers that nobody had expected or even imagined would arrive. One of the most significant of these is Apple’s new Metal API to greatly improve graphic performance by removing the limitations and performance impacts created by using OpenGL. Another, and arguably the most significant of them all, is Swift. Swift is Apple’s new programming language. After four years in development and without a single leak, Apple has created and now revealed what will become the foundation of a new generation of applications for both OS X and iOS.

Safety, modern syntax, and powerful features are three design principles that guided the development of Swift and it really shows. There are huge benefits for developers which translate to benefits for users. Naturally because of that, the first thing to discuss is why Swift is great for programmers before discussing why it will be great for users.

Modern Syntax for Modern Code

Being a brand new language, Swift strives to have a modern syntax that enhances code legibility, and works against bad programming habits. One of the most significant of these syntax changes is the elimination of the use of semicolons at the end of statements. For those who are unfamiliar with programming syntax, a statement essentially states an action to be performed. For example, the statement “X = 5;” would mean to set the variable X to be the number five. In many languages such as C, Java, Flash ActionScript, and of course Apple’s own Objective-C every statement must be ended with a semicolon. In Swift, Apple goes for an approach similar to languages like Python where it is not necessary to end statements with a semicolon. While this may be a difficult change to get used to for programmers used to programming in Objective-C, it will greatly reduce the frustration involved with errors due to the programmer simply forgetting to insert the semicolon at the end of a statement.

Another massive improvement comes in the form of new variable declarations and definitions. Traditionally in programming languages a variable is defined by calling it a variable, setting a name, setting a type, and setting a value. For example, the line “var year: = int “2014”;” indicates that a variable named year is being created and that it is an integer with a value of 2014. In Swift Apple has included support for type inference. Essentially, it is no longer necessary for a programmer to indicate what type of variable they are declaring. Based on what the variable is being declared as, Swift is able to infer what type of variable it should be. This means that the previously mentioned line can be shortened to simply read “var year: = “2014”.” This applies to any variable types and also extends to data structures such as arrays where an array of names can simply be defined as var names = ["Elaine", "Daniel", "Jash"] and Swift will know that it is an array of strings.

Now of course, the word variable implies that the programming is making a value they intend to change. But what if the programmer is declaring, for example, an integer that should always remain constant? In Swift the programmer is able to define an immutable constant by using the definition “let” rather than “var”. This is a big part of Swift’s new syntax. Immutability is preferred for values that are not going to be changed. From a visual standpoint, defining a constant makes it very clear to anyone reading over code which values are being altered and which are not. From a performance standpoint, declaring values as constants allows the compiler to better optimise code as it knows which values will be changed and which will remain the same. There are also safety benefits to this which will be touched on more later.

There is one final thing to touch on relating to the new code syntax. With Swift, a programmer can assign essentially any Unicode character to be the name of a variable. This allows the use of symbols like the Greek letter theta to represent an angle which further improves readability and effectively communicates what a variable is intended to be. In addition, it also supports emoji and so programmers can assign an emoticon of a banana to represent the word banana should they see fit.

The Programming Power Tools

Swift wouldn’t be a brand new language without some brand new tools and abilities for programmers to take advantage of. Any programmer who uses Apple’s Foundation Framework will be pleased to know that in Swift all typical variables are able to make sure of Foundation methods, and all methods that return a variable return a standard Swift variable rather than an NS variable.

Swift also includes many new ways to modify variables. With Swift strings, a programmer is able to append characters to the end simply by adding them much like accumulating one integer into another. For example, if the programmer takes the string “var name: = “Elain”” and the character “var end: = “e”” they may then write the statement “name += end” and the string will become “Elaine”. This is a stark contrast to Objective-C which would require the use of the NSMutableString type and much more code. This method also works for other data types. Strings may be added together, and arrays can also have values or even sets of values added. If any of the objects are defined using “let” rather than “var” the compiler will give an error about attempting to alter immutable objects.

One of the most powerful features of Swift is something Apple is calling Generics. Generics allow the programmer to create a function that performs a generic task that can work for multiple variable types. For example, a programmer may create a function that adds two variables together, and they may be integers, doubles, strings, etc. The programmer can create a function with a placeholder type name enclosed in angle brackets after the function definition. All variables passed to the function will be created under the placeholder type and the addition operation will be performed on them. This allows the programmer to have a generic piece of code that can perform the same operation on whatever variable types are passed to it, rather than having a function that is limited to accepting a single type of variable.

Safety

One of the big points emphasized with Swift is that it’s safe. Apple’s SSL bug that was revealed earlier this year is a prime example of how such a small code issue can be catastrophic and how it can be missed for quite some time. The explanation of Apple’s SSL bug is rooted in programming syntax. Traditionally when a programmer writes an “if statement” they are evaluating a statement to see if it is true or not. If it is true, the code contained within the if statement is run. Typically an if statement is opened and closed with brace brackets to designate what code is within it. However, in some programming languages if the if statement is resolved in one line it is not necessary to put brace brackets. This is where the security issue arises. When written with brace brackets, the part in the above code where the error occurs should read like this.

if ((err = SSLHashSHA1.update(&hashCtx, &signedParams)) != 0) {
          goto fail;
}
goto fail;

As you can see, no matter what occurs with the if statement, the second goto fail line is always reached and the SSL verification is terminated. While this bug was likely created by some sort of accident relating to copying and pasting code, it would have been much easier spotted during review had the brace brackets been there to show how the code actually should appear. Clearly Apple is aware of this sort of issue and with Swift they have addressed it. In Swift the programmer must always include brace brackets to open and close an if statement which prevents a whole slew of bugs that can occur due to what is honestly just sloppy programming practices.

Another safety aspect relates to something touched on earlier which is immutability. In programming there is a concept known as thread safety which requires shared data structures to be altered in such a way that execution by multiple threads at the same time will not cause errors. With Swift pushing for all objects that will not be altered to be declared as immutable constants they ensure thread safety for those specific pieces of data as they are not being altered and can be executed in code on multiple threads safely. Swift also ensures that you know what type of data is held in variables and data structures. For example, in the aforementioned array of strings var names = ["Elaine", "Daniel", "Jash"] Swift would throw an error should the programmer attempt to add anything other than a string to the array.

One of the final safety related features relates to switch statements. Switch statements can function in a similar way to if statements in that they test a variety of possible scenarios. If implemented poorly this can open the door for code safety issues if a programmer creates a switch statement that does not satisfy any of the given possibilities. For this reason, Swift requires that all switch statements include a default statement which will be run if all other cases fail.

What This Means for Users

Anyone reading this who is not a programmer may be wondering how this impacts them as a user. Well in fact, all of these changes that Apple has made to make things easier for developers also make things easier for users. Apple’s improvements to syntax will eliminate a great deal of time that developers spend resolving small bugs that are hard to spot and relate to a silly issue like a missing bracket or the semicolons that cause annoyance to no end. This means that a developer can find and resolve issues faster which in turn means faster releases and updates for the user. It also means the developer can spend less time debugging and more time doing what they want to do which is add and improve the features in their applications.

Apple’s new tools and methods for writing code allow developers to perform actions more easily and with shorter, more concise code. For the end user this could potentially mean new features that had been more difficult to implement in the past, and it most certainly means that apps will be faster than ever before. In fact according to Apple, Swift can be nearly twice as fast as Objective-C in complex sorting tasks and in performing encryption.

Apple’s safety improvements also serve to benefit the user by preventing issues with incorrect data input that the programmer may not have foreseen. By trying to protect the language from common programming errors relating to variable mutability, thread safety, and input, the user is much less likely to encounter a scenario where a crash occurs in an application.

We’re still learning about Swift. According to Apple 370,000 people downloaded the handbook on the iBooks Store only a day after it was unveiled at the WWDC Keynote. As people work through the handbook and begin to create new applications and augment their existing ones using this powerful new language both the users and Apple themselves will finally see the fruits of their four year labor. For anyone interested in learning more about Swift and what it offers you can download the official Swift handbook on the iTunes store.

Comments Locked

79 Comments

View All Comments

  • ArmanUV - Thursday, June 12, 2014 - link

    When you say "One of the most powerful features of Swift is something Apple is calling Generics", it implies that Apple is introducing this new exclusive feature, even though generics are a common feature of many other programming languages (https://en.wikipedia.org/wiki/Generic_programming)...
    It is almost like saying "One of the most powerful features of Swift is something Apple is calling Functions"!
  • Deelron - Thursday, June 12, 2014 - link

    No, it it states it's a powerful new feature that didn't exist before, which it absolutely is. It's no different then saying "this <car type> features a powerful new engine <that's more powerful then the previous one but came on a different model before>."
  • tdlf - Wednesday, June 18, 2014 - link

    Except Apple never said any of this. This is a poorly-written article. All Apple ever said was that they're including Generics in Swift. They never even used the word, "introduced."
  • MrSpadge - Wednesday, June 11, 2014 - link

    Yeah.. I also hoped to find something intersting here. Sure it's better than that language which starts with "C" and ends with "obol".. but not much more.
  • kushiku - Wednesday, June 11, 2014 - link

    Elimination of semi-colons, Type Inference, String commutation - aren't all these already present in Python. Generics? Oh come on...so many languages have that feature
    Atleast from the features mentioned in the article...I don't find anything new woth revolutionary
    Its just old wine in new bottle...fancy names and a brand called Apple
  • Brandon Chester - Wednesday, June 11, 2014 - link

    I think people are misunderstanding the point of this. Swift is only for people who program in ObjC, it's not intended to attract people who program in other languages because that would involve locking into Apple's platform. Your way of thinking puts Apple in a damned if they do, damned if they don't scenario. If ObjC stays the way it is it gets even more stagnant which hurts Apple developers and users. If Apple tries to remedy issues by creating something like Swift then people complain that they're all just features from other languages.
  • elsumidero - Wednesday, June 11, 2014 - link

    I think that no one have a problem with Apple launching a new language, the problem is how the article is written. With phrases like "One of the most powerful features of Swift is something Apple is calling Generics".
    Apple and EVERYONE is calling it Generics. Apple did not invented Generics, there were Generics before Swift. But writing the article in this way it may suggest to the reader that Apple invented all those features that were already present in many of other existing programming languages.
  • Arbee - Wednesday, June 11, 2014 - link

    Yeah, Apple didn't invent any of this naming, and they didn't claim to. It was a known thing for years that Objective C was increasingly out of sync with modern programming conveniences (Ars Technica in particular was sounding that alarm early) and this fixes it in a pretty clean way. There was a rumor at WWDC that it'll be open sourced as part of the next LLVM major release too, which would be terrific.

    BTW, this article misses one of the significant points about Swift: it can also be used as a batch/scripting language complete with the traditional #!/bin/swift or whatever header, and Xcode's Playground feature uses it in that mode to provide the sort of immediate-response environment that all of us who learned programming in the 1980s on BASIC for granted. Except on steroids: no 1980s BASIC would graph the value of a variable over time for you, or show the actual picture file that you just loaded.
  • lmcd - Wednesday, June 11, 2014 - link

    Given the .NET roots of PowerShell, I wouldn't be surprised to see Microsoft quickly build the equivalent features for PowerShell.
  • ex2bot - Wednesday, June 11, 2014 - link

    Somehow, I don't think Apple developers will be able to take advantage of PowerShell. :)

Log in

Don't have an account? Sign up now