Currently, I am making some effort to improve my iOS skills and become a “full-fledged” iOS developer. Back in 2013, when I first started to get interested in iOS, the choice was simple. If you want to become an iOS developer, learn Objective-C. That is what I set out to do.

However, in June 2014, Apple introduced a new programming language: Swift. That meant that there were suddenly two options for making native iOS apps: sticking with the older and more established Objective-C or learning the new, hyped and (at that time) unstable Swift.

At first, I decided to stick with Objective-C, as it had been around for longer and was the language used by companies developing apps. Swift was reported to change a lot with every update - which makes it hard to learn for a beginner developer.

However, with time, the contemporary and the multi-paradigm language Swift became more and more appealing. I noticed that more and more people (and companies) started to adopt the language and be amazed at its possibilities.

The syntax is arguably simpler than for Objective-C and the combination of object-oriented and functional programming concepts makes it exiting and fresh. Swift is both similar to and different from many programming languages, adopting useful ideas from Objective-C, Haskell, Python, Ruby and others.1

A lack of books

Introducing a new programming language indeed changes the market. As a beginner regarding iOS development, I was looking for books to cover Objective-C and/or Swift. Since I found a course that would teach app development using Objective-C, I set out to find the latest and most up-to-date books for that language.

However, the introduction of Swift meant that book writers were busy writing entirely new books for Swift rather than the next edition of their Objective-C series. For a while, there was a lack both of good Objective-C books and of good Swift books - at least if you wanted an up-to-date book covering the latest iOS.

Early this autumn, when iOS 9 was about to be released, many books where still discussing iOS 7. I ended up buying the 4th edition of “iOS Programming: The Big Nerd Ranch Guide” (Objective-C, iOS 7) and the “iOS 8 SDK Development” book (covering Swift). While they were already a bit outdated due to the new iOS 9 and Swift versions - they are still really good books for learning about iOS concepts and development with a hands-on approach. Now, a bit later in the autumn, Swift books are starting to appear from many sources (and hopefully there will be some covering Objective-C too?).

A mishmash

So, as a result, I am now learning Objective-C and Swift simultaneously. Even the course that was supposed to cover Objective-C changed its approach midway and started to focus more and more on Swift.

While the languages differ in many senses, Swift and Objective-C are interoperable, meaning that code written in one of the languages can be accessed and used from a file in the other language. This has the benefit that you can use Objective-C APIs when programming in Swift.2

There are many iOS frameworks that could be used in either language, so a developer knowing Objective-C will easily implement something similar in Swift. For example, consider using a UITableView (a list) with a UITableViewDataSource. The data source method tableView:cellForRowAtIndexPath: is used to populate the table. A simple implementation in Objective-C might look something like this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    // get a cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: @"ItemCell" forIndexPath:indexPath];
    
    // get an item from an array
    Item *item = [self.items objectAtIndex: indexPath.row];
    
    // configure the cell to show the item
    cell.textLabel.text = item.name;
    cell.detailTextLabel.text = item.characterization;
    return cell;
}

Now, lets show a similar implementation in Swift:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
    // get a cell
    let cell = tableView.dequeueReusableCellWithIdentifier("ItemCell", forIndexPath: indexPath)
        
    // get an item from an array
    let item = items[indexPath.row]
        
    // configure the cell to show the item
    cell.textLabel?.text = item.name
    cell.detailTextLabel?.text = item.characterization
    return cell
}

Actually, the UITableView inherits from the Objective-C class NSObject, but interoperability means that the Swift code can access any properties and call any methods on the object.2 Notice that in Swift, there is no need to explicitly declare the type of a constant or a variable, as long as it can be inferred by the compiler. The let keyword means that we are defining a constant (as opposed to a variable, var).3

When calling methods, Swift uses dot-notation and parameters in parentheses. You should also note the question mark in cell.textLabel?.text, indicating that the textLabel property is an optional. An optional could either contain a value or nil if the value is missing.3 In this case it is safe since the optional appear to the left of the equals sign, meaning that if the expression evaluates to nil we will not assign to anything.

At first I was a bit confused by the similarities and differences when using Objective-C and Swift syntax, but after a while they made sense and the languages became easier to separate from each other.

While I consider this comparison to Objective-C and acknowledging the common ground as a first useful step towards learning Swift, I would also like to explore the parts that separate it from Objective-C, like for example the functional programming paradigm (I got a taste for that from an Erlang course). There are a lot of things left to learn to become an advanced Swift user, but as always - you have to start somewhere - and the sooner the better. Currently, I am working on a project that leverages some of these basic Swift and iOS skills to build a To-Do/productivity app.

More on that in a later post…

  1. Wikipedia. Swift (programming language)

  2. iOS Developer Library. Interacting with Objective-C APIs 2

  3. The Swift programming Language (Swift 2.1 Edition), Apple Inc, 2015.  2