Apple has released a new programming language. It’s called Swift.

This is Apple’s summary:

Swift is a new programming language for iOS and OS X apps that builds on the best of C and Objective-C, without the constraints of C compatibility. Swift adopts safe programming patterns and adds modern features to make programming easier, more flexible, and more fun. Swift’s clean slate, backed by the mature and much-loved Cocoa and Cocoa Touch frameworks, is an opportunity to reimagine how software development works.

It’s the sentence in the middle that really gets me. It’s certainly a more advanced language but I’m not sure about easier or more fun.

Note: Both of the examples below are pseudo-code. They will not function without additional code.

Here is an example:

class DataManager {

    @lazy var managedObjectContext : NSManagedObjectContext = {
        [unowned self] in
        var mo = NSManagedObjectContext();
        mo.persistentStoreCoordinator = self.persistentStoreCoordinator
        return mo
        }()

}

Can you tell what the above is doing? Probably not without direct Swift experience. Here is the equivalent Objective-C version of the same code

@implementation DataManager

@synthesize managedObjectContext = _managedObjectContext;

- (NSManagedObjectContext *)managedObjectContext
{
    if (_managedObjectContext != nil) {
        return _managedObjectContext;
    }
    
    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];

    _managedObjectContext = [[NSManagedObjectContext alloc] init];
    [_managedObjectContext setPersistentStoreCoordinator:coordinator];

    return _managedObjectContext;
}

Even non-programmers have a bit more of an idea of what the second example is doing.

I’ll break it down for you.

Both of the above are examples of something called “getters” in programming. In object oriented programming, objects have parameters that define them. Let’s say you wanted to write a program that inventories specific items in your fridge. You would have a Fridge object which has a count for eggs and cans of soda.

class FridgeObject {
     var eggs = 12
     var soda = 12
}

The above represents a fridge object that starts with a dozen eggs and a dozen cans of soda. In fact, the above is fully functional Swift. You can access this by doing the following:

let fridgeInstance = FridgeObject() //Creates an instance of the default fridge that can now be set. It will have 12 of each to start with. You have to have an "instance" to manipulate values. I named it that way to make it easier. Objects and Instances could be named anything. They don't need to contain the word object or instance.

fridgeInstance.eggs = 10 //Sets the number of eggs to 10. So you used 2.
fridgeInstance.soda = 8 //Sets the number of cans to 8. So you used 4.

A getter simply returns the current value stored in the object. Like so:

fridge.soda //Based on above, this would output 8

Simple, right? But why are the above examples so convoluted?

On mobile devices, memory and processing is very limited. Often, you don’t want to create something until right before you need it. And, more than that, often you want to create something once, and not every time the function is called.

If you look at the Objective-C version, you can see the first if statement checking if the object exists and returning it if it does. This causes the code below the if statement to be skipped. It’s pretty clear. If it doesn’t already exist, it creates it then returns that.

What the hell is Swift doing?

Well, the same thing. In a very Swift like way. First, it’s marked as @lazy. This tells the program to wait until the variable is used to create it. This is for memory efficiency and performance. It’s particularly useful when that piece of code is not often needed.

The var managedObjectContext : NSManagedObjectContext = { ... }() is saying set the value of this variable to the return value of the code between the braces. That’s really it. The last parentheses, () are because this is technically an inline function. It’s like English grammar. Don’t ask questions, just accept that it is the right way to do it.

The magic is that the code in the braces is only executed once. And only if the variable isn’t set to something else. Let’s modify the refrigerator example:

class FridgeObject {
    @lazy var eggs:Integer = { return 12 }()
    @lazy var soda:Integer = { return 12 }()
}

Yep, the above is functionally equivalent to the previous refrigerator example. Essentially, those brackets {} allow you to do more complicated one time initialization in this case. Since it is so simple, we didn’t need this syntax.

Ok fine, I’m confused now. Why Swift?

That’s the thing: Swift is full of all these new language specific syntaxes. It is a modern language that can handle many things Objective-C can’t. That said, its syntax doesn’t make programming more “fun” or “easier” to me. It does take fewer lines of code to accomplish the same task at the cost of making things harder to understand initially. The above code in Objective-C would be 40-50 lines that are much easier to understand. It is unusual for Apple to choose a harder learning curve for long term efficiency over an immediately usable product with some inefficiencies.

That is interesting to me. Is it a sign of large scale changes at Apple? Or is this simply what developers have to deal with?

What do you think? Reply below

Tagged with →