Apple’s Swift: A Developer’s First Impression


Our developers Jordan Clifton and David Sweetman dove into the new programming language, “Swift,” that was announced yesterday during Apple’s keynote at WWDC14. After hearing the announcement and viewing the demo we were excited to jump in and start learning more about Swift using the Playground. Playgrounds are used to experiment with new technologies, analyze problems, and prototype user interfaces.

Developers watching WWDC14

Developers watching WWDC14 in the new office.

Swift uses the same runtime as Objective-C under the hood, and also uses Automatic Reference Counting (ARC); in fact, in the book “The Swift Programming Language,” Apple suggests that a major motivation of ARC was to pave the way for Swift. Swift also offers seamless access to existing Cocoa frameworks and is fully interoperable with existing Objective-C code.

Here are some comparisons between equivalent operations in Objective-C and Swift:

Array initialization:


 NSArray *myArray = [[NSArray alloc] init]; 


let myArray = String[](); 

Both lines of code create an empty array using the initializer syntax.

Creating an array with items also looks quite different:


 NSArray *myArray = [[NSArray alloc] initWithObjects:@"one", @"two", @"three", nil]; 


 var myArray = ["one", "two", "three"]; 

Another exciting element of Swift is how clean and easy it is to read the syntax. This allows developers to produce more maintainable code while having fun doing so. Additionally, Swift includes a for-in loop that makes it easy to iterate over arrays, dictionaries, ranges, strings, and other sequences. The loop can use exclusive and inclusive ranges, and parentheses are optional. Traditional for loops are also available to use as well.

Using the for-in loop in Swift would look something like this:

for index in 1...5 {
   println("index: \(index)")

This will loop through numbers 1-5 printing what the current index is. This syntax is another way Swift has successfully increased readability making it very easy to understand what the code does. At first glance, Swift seems to bring a nice mix of syntax and features to Apple development that will likely help write concise code and express instructions in ways that that were not previously possible.

One of the most exciting parts of Swift is its functions and methods. In Swift, functions are first-class types. Compare that to Objective-C, where you have methods which must belong to a class. If you want to create a function outside of a class in Objective-C, you have to use C functions or blocks.

The below example demonstrates a class that defines a simple class/type method to add two integers. Notice that Swift obviates the need for a separate interface:


@interface MyClass : NSObject
+ (INT) addValue:(INT)value1 toValue:(INT)value2;

@implementation MyClass
+ (INT) addValue:(INT)value1 toValue:(INT)value2 {
   return value1 + value2;


class MyClass : NSObject {
   class func addValue(value: INT, toValue: INT) -> INT {
      return value + toValue

The above demonstrates basic Swift function syntax:

func functionName(firstArgName: arg-type, secondArgName: arg-type) -> return-type {
   // code block

The Swift type method in the MyClass declaration above would be called like this:

var value = MyClass.addValue(5, toValue:4)
// value = 9 

Calling the method highlights an interesting naming idiom suggested by Apple: in methods, the first parameter is not assigned an external name automatically, but subsequent parameters are, so we pass it the parameters as `5, toValue:4`. `toValue` is the external name of the second argument, and is included for clarity, to give the same readability that Objective-C does by interspersing arguments in the method name. The first parameter “5″ does not need an external name, because the method name itself sufficiently describes the first variable.

It looks like things will get pretty fun with functions in Swift: currying, variadic arguments, multiple return values. Definitely take a look at the “Functions” and “Methods” sections of the Swift Programming Guide to learn more of the details.

This article is simply a brief look at how Swift might change the face of iOS development. There are many more awesome features to come, and we can’t wait to dig in and start coding! What do you think? Give your two cents below.

| | |

16 Responses

  1. Ben says:

    Thanks for the comparison. You should look at your code blocks in the article though: the html line break tags are rendering putting all the code on a single line.

  2. Mark says:

    You should have used the Objective-C Literals syntax for the arrays, since that seems to be the defacto way of writing this now in Objective-C.

    NSArray* myArray = @[];
    NSArray* myArray = @["one", "two", "three"];

  3. SteMc says:

    let myArray = String[]();

    You’ve initialised an empty array with a constant here, nothing can be appended to that array at a later date. Maybe initialising it with var would make more sense?

    Nice work though.

  4. Drew McA says:

    Just as a clarification: you can actually define arrays in the current version of Obj-C quite easily:

    NSArray* myArray = @[@"one", @"two", @"three"];

    But Swift does do away with those pesky “@” symbols!

  5. […] Apple’s Swift a developer’s first impression is a very interesting first-account report on using Swift for the first time. […]

  6. MarkB says:

    class MyClass : NSObject {
    class func addValue(value: INT, toValue: INT) -> INT {
    return value + toValue

    1) Don’t think you need to specify NSObject as the base class.
    2) There is no type INT. I’m guessing that was just a typo.

    • Jordan Clifton says:

      Thanks for reading MarkB.
      1) Yep it’s just an example.
      2) Yes, it was a formatting issue with the site

  7. Jayendra says:

    Thank you JORDAN CLIFTON !!!

    Nice comparison….But want to tell you “swift” doesn’t allow you the semicolon(;) in its syntax…

  8. […] up Swift code against Objective-C code. For instance, developer Jordan Clifton earlier this week posted a few examples which make the point rather […]

  9. Jen says:

    You’re comparing less than optimised ObjC to nice clean Swift. A better example would have compared:
    let myArray = String[]();
    NSArray * myArray = [NSArray new];

    Which still creates an (arguably redundant) empty immutable array.

    and you should have compared:

    var myArray = ["one", "two", "three"];
    NSArray * myArray = @[@"one",@"two",@"three"];

    Showing the worst of one language against the best of another isn’t fair. You should have compared best vs best.

    Swift looks real nice, but you should do a fair comparison.

  10. James says:

    Would anyone recommend learning Swift or start with Objective C first? I feel that learning C would give me a better understanding of the internal workings and will lead to better code, but it will take much more time and has a steeper learning curve.

  11. Ok. Apple (with it’s Objective-C) was behind Oracle (Java) and Microsoft (C#) for a decade.
    Now Apple reinvented the wheel – simply merged C#/Java, and a little of Delphi and Basic.
    Will Maczombies eat it? Sure they will! “Apple’s Swift is clean, easy, readable, amazing, incredible, exiting, awesome, innovative”.

Leave a Reply