Swift and Shift - The new language from Apple??

Come WWDC and Apple brought in a big surprise, using the word Big would be an understatement because it is bigger than the biggest big as far as Apple are concerned. They have changed or rather want more developers to jump on to the bandwagon by offering a better solution than Objective-C

What's Out there

The only way that a developer can create an app for the Apple (officially using the prescribed options by Apple) is to use Objective-C. Objective-C has its own strengths and its own weaknesses.

and if you are wondering about the Title, that was an Australian comedy show on SBS (a TV channel) you can know more about at http://en.wikipedia.org/wiki/Swift_and_Shift_Couriers


With C++ and Objective-C not being the entry point for many, there were many options pitched at the developers by third parties. The options ranged from Drag-Drop no code options to modern languages like Python, Ruby, Lua to other options like ActionScript, JavaScript and HTML. These languages brought their own advantages and disadvantages, like figuring out the indentation of a nested loop in Python that can drive you crazy. There were also other options that allowed the use of DotNet languages (Xamarian) to create iOS apps or even RubyMotion. Some others changed from C++ to easier options using simpler interfaces and languages.


Of the many options that are available the deciding factors often are determined by a couple of factors like is the language mature and accepted, what does it cost not just for the framework but also for getting developers and support in the long run. With prices like $299/Year for Xamarian (Indie), $199.99/Year for RubyMotion it is not cheap specially when compared to other options like Moai, Gideros, Corona that use Lua and are FREE for basic stuff. Many others are now offering starter options that are also free and some others have lowered the entry price.


Right, so where does Swift fit in with all of this and how does it measure up? Firstly it is an official offering from Apple. Secondly it comes as a replacement for Objective-C which means it is natively supported with Apple tools including debugging, etc. Since it is a new language there are *supposedly* no real experts on Swift. However, the two factors that help a person become an expert are prior experience with the iOS and Mac SDK (at the end of the day it is all about using the classes provided) and the second is prior experience with a scripting language. There have been enough jokes on recruiters asking for experts with 5 years experience on Swift and how many are clamouring onto the band wagon to be pioneers in this gold rush.


Swift has a lot of similarities to scripting languages and therefore depending on what language you are familiar with, you could see it in that light. As the author of a Lua book, I feel that Swift has a lot of similarities to Lua. The first ones that you cannot miss which stare you right in the face is the use of an underscore for omitting variables that you do not want to use.

name, _ = getNameAndAge(studentID)

Another thing that if you have downloaded the Swift books from iTunes Store then you would have noticed that on Page 179 under the explanation for the While loops, the example used by Apple is of Snakes and Ladders (Chutes and Ladders for the Americans, perhaps?) and if you have read my book "Learn Lua for iOS Game Development", starting on page 51 under the "Using Math to Loop" I have the Snakes and Ladders as an example too on page 52.

The variables are no longer explicitly defined and are set implicitly which is a good start but are not variants which can be used to hold other types (as yet). Like in Lua where you can simple create a variable on the fly and not bother about it's type. Currently Apple does not allow that is because they attempt to keep Swift type safe and thereby the compiler can pick up the errors.

Arrays or Tables are like Lua that can be created easily but they are not as flexible as in Lua. While Lua allows creating mixed type tables (lists, or dictionaries) as can be seen in the code example below.

local tbl = {1, 2, 3, 4}
 tbl["One"] = "Eins"
 tbl.two = "Zwei"
 table.insert(tbl, "Drei")

With swift, you can either create a list or a dictionary (which closely resemble the NSArray or the NSDictionary objects)

Functions can return multiple results like Lua does however this is in the form of Tuples. The good bit is that the tuples can have named members. This is the same method that can be used in DotNet or other languages that do not support multiple return values by return a structure/tuple which has multiple members with the results.

Keep a look out for more details as the NDA lifts and as more information is allowed to be provided to all.

Why not Lua?

Lua is wonderful and I still use it, in fact not just for mobile development but also integrating it in applications. Lua works fine for me (a single language to learn) and is available in the frameworks that I want to use and even available for DotNet and exposes all of the classes and objects to the script, like the Wax framework for iOS but much better in terms of using automatic reflections.

Swift on the other hand is not cross-platform and only available for Apple systems (since Apple use gcc or llvm it could be a matter of time before they *MIGHT* open source swift and it could be integrated onto many other platforms).

In fact if you have seen the keynote video, you will notice that the translucency now scrolls the items under the titlebar of the window, which means the clipping frames have just moved slightly up. What does that have anything to do with Lua? Well a lot of developers that did not have access to the native components build the same with layers and while some frameworks did not have masks or have masks that worked as advertised, the lists were created in layers with the titleBar covering the scrolling layer.

The Future

While it is similar to a lot of modern languages, I might be repeating myself when I say that there exist tools that allow for debugging and are better integrated. Lua is brilliant and I would continue to use it. If it was not any good why would there be so many frameworks that have used and continue to use Lua. Starting from LucasArts to World Of Warcraft, RoboBlox, Corona, Gideros, Moai, Marmalade, Love, Codea, Lego Mindstorms NXT, nginx, wireshark to name a few. However, there are not many tools that allow for debugging and the security of the code and most important access to the native objects available in Objective-C.

Yes, we do need some cross-platform options and there are options like Juice and Appportable both of which compile Objective-C for Androids. We will continue to use Lua for Mobile development given the frameworks that we love, but Swift does usher in new hope with the same ease of Lua.

I would have loved to put in some code examples of Swift, but am not sure about the NDA restrictions. Apple has made the Swift book available for everyone (not just developers) still there are NDA restrictions in place.

Where is it better than Lua?

One thing that I simply loved about swift is extensions. Int, Float, Bool are all types or classes, not real primitives. With extensions, you can extend the functionality be adding new functions appended to them. If you are confused, here's a code example to illustrate what you can do with swift.

2.repeat({println("Jingle Bells")})
 println("Jingle all the way")

where we write a new extension function called repeat that extends Int and repeats a task number of times indicated by the int.



More information / Resources

Apple has plenty of resources and there are a couple of YouTube video tutorials to get you started. Links updating soon.

UPDATE: Please note that like most applications compiled with Apple's compilers, the text strings are stored as plain text and are exposed in any hex editor or other tools like class-dump. So be careful about your sensitive information and passwords, etc that you include in your apps. To be fair, even the apps compiled with Objective-C displayed the same so, there's nothing new and alarming in this, but as a developer keys, passwords that you would use for API's, use carefully or encrypt them or obfuscate them.


Popular Posts