Swift – a language created by Apple, joining the traits of a compiled language and a scripting one. Working in real time. Much faster than Objective-C, whose syntax was never very clear. What is the new swift language like? What does its syntax look like? Below are the answers and the material available for Swift![hr]

At the WWDC 2014 conference Craig Federighi announced that Apple had created a completely new language, one carrying no leftovers from the old Objective-C. Objective-C goes back as far as NeXT. Its syntax was never very readable, and only the Xcode development environment made it possible to write in it. Suddenly it turns out that all of this has been changed.

The official press release

According to the official press release, Swift is a completely new language for iOS and OS X. It is meant to make writing software much faster and more comfortable. Designed specially for the Cocoa and Cocoa Touch frameworks. It is supposed to join the performance and speed of ordinary compiled languages with the comfort and simplicity(?) of scripting languages.
Swift is meant to help developers write code that avoids the more annoying errors. Old apps built with Objective-C will be easy to integrate with the completely new Swift. The Xcode development environment will make Swift extremely useful, and the developer will have a live view of how the app behaves while the code is being written.

This marketing gibberish says essentially one thing. The developer will have an easier, better life. How will it be in practice? That is coming in a moment!

The new language has high level structures and a lot of conveniences. Apps can only be submitted once iOS 8 and OS Yosemite are officially out (autumn).

A quick look at Swift

  • A compiled language, implemented with LLVM (Low Level VirtualMachine).
  • Automatic memory management. It runs through ARC (Automatic Reference Counting), as it did in Objective-C.
  • Much friendlier for developers coming from languages like C# or Python.
  • It brings many new high level structures, such as generic types and protocols (a generalization of interfaces in other languages). A fuller description below.
  • Easier iteration over the values in a collection.
  • Its structure supports the functional programming paradigm.
  • Dynamic polymorphism. Methods can return several value types at once. Methods can have optional parameters.
  • Easier to learn programming for completely new developers.

The Xcode 6 development environment

A lot of new functionality has been added. New developers in particular will be pleased.
Xcode 6 will contain a special environment called „Playgrounds”. It will be there for experimenting with code. An interactive debugger will make the whole thing run in real time. With the special REPL environment (Read, Eval, Print, Loop) developers will be able to program „line by line”.

Swift programming languageWhat matters is that Swift code will be usable freely right next to C and Objective-C. I wonder what that will do to the readability of the code? :D

Swift – the characteristic features

The list of characteristic features (a quick cheat sheet) for Swift was put together with running help from Adam Śliwakowski. Thanks!

  • No need to add a semicolon. This is enough:
    println(„Hello world”)
  • The word „let” lets you define constants.
  • The word „var” defines ordinary variables, the same as in JavaScript
  • You can return several values :D
    return (val1, val2, val3)
  • No need to use brackets around conditions
  • After the variable name you can state the type, but you do not have to
    let twojaZmienna: Double = 60
     // other examples of defining variables
    let label = „The value is:”
    let width = 94
    // adding a numeric value as a string:
    let widthLabel = label + String(width)
    // or this way:
    let widthLabel = label + \(width)
    
  • Variables can be optional (they can be null)
    var optionalString: String? = „Hello”
  • Creating arrays is very simple
    var shoppingList = [„catfish”, „water”, „tulips”]
     \\ access and assignment of an array element:
     shoppingList[1] = „cola”
    
  • Creating a dictionary is as simple as an array
    var occupations = [ „Malcolm”: „Captain”, „Kaylee”: „Mechanic” ]
     \\ access and assignment in a dictionary
     occupations[„Jayne”] = „Public Relations”
    
  • An empty array and dictionary with the type / types stated
     let emptyArray = String[]()
     let emptyDictionary = Dictionary()
  • An empty array and dictionary without the type stated
    let emptyArray = []
         let emptyDictionary = [:]
    
  • The „switch” construct can handle different types
    Switch construct
  • The for-in loop – iterating through a dictionary with a key and a value
    For-in Swift
  • The for-in loop – going through a range defined as:
    for i in 0..3 { } \\- contains 0,1,2 ( two dots )
    for i in 0...3 \\- contains 0,1,2,3 ( three dots)
Functions in Swift
  • The word „func” – declares functions. The shape:
    func nazwa_funkcji(param1: String, param2: String) -> String { }
    
  • The „->” sign separates the parameters from the type of the returned object
    functions Return Type Swift
  • Returning several values – return (val1, val2, val3)
    ReturningSeveralValues
  • You can take an unknown number of arguments (the arguments come in as an array)
    Swift arguments
  • Nesting functions. Lower level functions have access to variables defined in the „parent” functions.
    swift parent functions
  • Functions can return other functions as values!
    Swift Functions Return Other Functions
    And what if the first function returns the second, and the second returns the first? :D
  • Closures. Functions are assigned to concrete values without being created together with a name.
    Closures Swift
Classes in Swift
  • The word „class” – declares a class. Variables, constants and functions are declared as before, but here they exist in the context of the class
    Classes in Swift
  • Deinit – the function called as the deinitializer. You can add code to be run before deallocation.
  • Override -used when we override a function from the parent class. Known from other languages.
     override func simpleDescription() -> String { return „description" }
  • Getters and setters can be nested as functions next to the variable declaration.
    Getters Setters Variable Declarations Swift
  • Willset & didset – methods called before or after a variable’s setter runs. You can use them when you want to work on other variables at that moment.
    willset Didset Swift
  • Extension – the equivalent of „Category” from Objective-c. It adds functionality to existing types, such as new methods or changed fields.extension Swift
  • An example of generic functions, which take arguments of any type
    generics Swift
  • Where – added to the parameter list, it states the condition that the argument’s type must implement a protocol, must have a given superclass, or must be the same as the type of the second parameter.
    where Swift
  • Protocols – classes implementing a protocol must contain in their description the values and methods defined in the protocol
    protocol Swift

The list of materials, all of them in English

  1. The official introduction to Swift
  2. A quick tour of Swift
  3. Swift with Cocoa and Objective-C
  4. The full documentation for Swift
  5. The Swift book (iTunes)
    I will try to make a pdf version available soon.

That is all. A completely new subject and a very interesting one! It has enormous potential in it. I will try to prepare material regularly.

Author