Swift Journal: Day One

Swifts

Learning a language is sometimes confusing

I decided to jump right into Swift without doing any research into the writing done over the past month so that my opinions were not colored. My background is in the C/C++ and, lately, Objective-C of which I’ve become quite fond. I approached Swift with the hope that it would provide the same kind of  power I’ve found Obj-C/Cocoa to have, but with some modern newness brought in from other successful languages. It is too early, and the tools are too fresh, for me to draw conclusions on how useful it will be for production code other than that it does look promising.

TL;DR after the jump wherein I detail experiences with the Xcode playground and a small Swift based iPhone app.

Smooth

  • It took only about a ten minute glance over the language docs to be able to port my C code to Swift
  • Explicit control of nil behavior such as when to require non-nil objects
  • Enums are first class types along with classes and structs
  • Property system like Objective-C’s with custom getters and setters
  • Aspect oriented features on the properties,
    didGet

    and

    didSet

    handlers

  • The beauty of every callable block being a closure: global functions, nested functions, class methods and traditional lambda style closures
  • Syntax shortcuts when they make sense like leaving off
    get

    for a readonly calculated property or

    return

    for one line closures

Friction

  • Tools: the playground, compiler, all are early versions and have problems; but we know that, and that’s why we will be shipping Obj-C apps for a while
  • The syntax is a step back from the terseness of Objective-C towards the explicitness of C++ (Python users are laughing right now; and yes Obj-C names are wordy, read that as “readable”, but the syntax mostly stays out of the way)
  • No headers; wade through the implementation to find the declarations you need (somehow Swift’s type declaration file avoids this)
  • I don’t think I like the declaration style 
    var x: Int = 4

    which reads like “assign something to SomeType” to me

  • It took some fumbling to figure out how to define outlets and actions in my first Cocoa Touch app (where are these language extensions documented?)

(more…)

Beautify Your Objective-C Code

Uncrustify is an open source code formatting tool for various languages including C, C++ and C#. It can also be used to format Objective-C code.

Uncrustify offers a large number of fine-grained options for code formatting that can be somewhat daunting to set up. The tool I want to mention here is a GUI application written by Ryan Maxwell that provides a neat user interface for setting the options for code formatting for Objective-C code. It is an application called UncrustifyX.

Here is a screenshot of the main window. It is easy to change the setting of an option on the left side and see its effect on the code on the right.

Image

(more…)