Introducing Swift

At WWDC 2014 Apple introduced a new official programming language called Swift to replace the aged Objective-C. In this article we'll explore some of the similiarities and differences between Swift and RubyMotion.

RubyMotion vs Swift

First off, RubyMotion itself is a toolkit and chain to build iOS applications written in Ruby that is bridged with Objective-C. Entire applications or just static libraries can be created. Swift is just a new language supported natively by Apple and it's build tools.

Swift Impressions

I've looked through much of Apple's documentation on the newly announced language and have found several concepts that I welcome as changes to writing iOS or Mac applications over Objective-C, as well as some things that Ruby provides that I hope make it over to Swift in the future.

Welcome Changes

  • Ditches the bracket and semicolon syntax
  • Ditches pointers
  • Typing is improved and easier to use
  • Simpler object initialization and method calling syntax
  • Bridging for new String classes or even Selector instances is simple and intuitive
  • Extensions allow easily adding behavior to existing classes without subclassing
  • Closure/block syntax is improved
  • No need for NSString vs NSMutableString anymore. Declare your string as a variable using var or a constant using let and that determines mutability
  • String equality is finally achievable just via == operator
  • Typed arrays declared as String[]
  • Switch statements break on the first match without the specific need for a break call
  • Ranges!

Missing or Annoyances in Swift

  • Parenthesis are not optional for calling methods - would much rather write view.update vs view.update() especially
  • var and let seem archaic and odd to me. Would have much rather used Ruby's way here
  • Would have loved to see a method_missing implementation from Ruby for extra dynamicism
  • Do not like the println("my \(variable)") string interpolation syntax at all - just not a fan of using the backslash
  • Not a fan of intializing dictionary literals with brackets, ie: var dict:Dictionary<String, String> = ["key": "value"] seems out of place to me. Would have rather seen: var dict:Dictionary<String, String> = { "key": "value" }
  • func doesn't sit just right with me. I'm not sure what I would want in place, it just catches me off guard - not a big deal obviously.

Remaining Questions

There are a few things I haven't yet figured out and would love to know if you have the answers to.

  • Is there any callback such as the Module.included method, when importing a namespace?
  • Is something similar to define_method from Ruby available?
  • Do you have to include a superclass when defining a class with protocols? Meaning is the following valid? class MyClass: NSCopyingProtocol
  • Have I missed a way to implement method_missing? Guessing no since Swift uses the same runtime as Objective-C

RubyMotion

RubyMotion still has several perks, however one can definitely argue that the list was much longer before WWDC this year.

  • Any IDE of your choice
  • Extensive collection of Ruby gems from the community
  • More people know Ruby compared to Swift
  • I think Ruby will still be a more natural choice for defining intuitive and gorgeous DSLs
  • Support for iOS, Mac and Android (coming in version RM 3.0) with one language

Ready to have a chat?

Contact us to chat with our founder
so we can learn about you and your project.