A month ago, I was sitting in Moscone West, watching Craig Federighi presenting on stage. "This release is the biggest release since the launch of the App Store." when Craig said that, I was wondering if there would be a new device or not, but Francis who was sitting next to me got a much better guess, "Maybe a new language?"

It has been two months after Apple announced Swift, their new programming language. To get a taste of it, I have rewritten one of my apps, Hong Kong Ferries Timetable using Swift and opensourced it. Keep in mind that Swift is still in beta, everything could change.

Typed

One of the biggest feature of Swift is heavily typed, and

  • It helps me to find bugs.
  • If you are dirty, you can escape from it anyway
  • Writing expression become difficult. I just want to multiply numbers, I don't care about what are they. Especially Float and CGFloat, I know they are different things, but it is a new language, why not take this chance to simplify things?

Syntax sugar

  • Range: Nice, tho I seldom use it
  • Easy to learn, difficult to read?
    • foo?.bar?
    • foo!.bar!
    • doesnt?exist ?? default
    • These are difficult to read
  • No need to type self. improved readability
  • Block improved!
  • Getter and setter looks better
  • Enum's short-hand syntax feels good

Some other features

  • Pattern matching in switch is powerful!
  • Require override keyword when overriding super class is nice
  • Since initializers are not inherited, I have to write codes to call super, just to overcome the required keyword, it can be kind of redundant

Working with Objective-C

  • Bridging is easy
    • Can get Cocoapod to work easily
  • Classes and API are very similar to Objective-C's, you don't have to remember lots of new things.
  • You'll need some time to get used to type translation between Swift and Objective-C, such as:
    • var array:Array = [] is mutable
    • let array:Array = [] is not
    • let array:NSMutableArray = [] is mutable
    • How about AnyObject and NSObject?
  • Not everything is supported and it does break things:

No more @dynamic

There is @NSManaged, but that is only for CoreData
So you cannot extend CALayer to animate custom properties

@objc loses Swift's function

Some specific functions only work if you use the @objc attribute, but the tradeoff are Swift-only functions.
For example you will need @objc to check for protocol conformance

You can check for protocol conformance only if your protocol is marked with the @objc attribute

And also @optional

Optional protocol requirements can only be specified if your protocol is marked with the @objc attribute. Even if you are not interoperating with Objective-C, you need to mark your protocols with the @objc attribute if you want to specify optional requirements.

More to go.

Maybe you will need to think twice before designing API, you couldn't use some nice feature like enum with raw values.

Playground

Playground is a really nice way to try out Swift. And it helps me to debug graphic-drawing code as well. With playground, I can debug code in drawRect: easily, seeing them draw step by step. However playground is still not stable.

Xcode

Xcode is not ready yet, at least for now

  • Autocomplete in Swift is slow
  • Doesn't have autocomplete for enum's short-hand syntax, which makes me type the its full name
  • Sometime shows unrelated error messages

Easy to start

If you understand Objective-C

When Swift is announced, Apple did not only prepared a full documentation, they also wrote a book to explain everything very well. When you browse Objective-C headers in Xcode, it even create a Swift version so you can copy method names easily. Although the community is still young, there are a number of StackOver posts that would guide you through problems.

Not that great for beginners

If you understand Objective-C, you have much greater chance to find a solution than Swift's. Afterall, you have to understand Objective-C to fully leverage the platform. Just like even thought there is ARC to help you to manage memory, you will have to understand what is retain and release, instead of writting strong everywhere.

Article Image from http://www.flickr.com/photos/lipkee/4916308411/in/photostream/

Blog Logo

b123400


Published