EDITING BOARD
RO
EN
×
▼ BROWSE ISSUES ▼
Issue 26

Let’s Swift

Valentin Filip
Cluster Manager & Team Lead on Innovation
@Yonder
PROGRAMMING

Apple is known for always trying to impress with the best quality in their products and services. They have done it again, once the new programming language called Swift was revealed. It has been in the works for the past 4 years, and while it started as a side project for Chris Lattner, an employee of Apple, it has easily gained trust with the top execs of the company, having assigned a great team to help finishing it.

On June 2nd, Swift was released to the current Apple developers, with the promise of better performance and higher efficiency. Considering the latest updates made to Objective-C, most of the public was surprised of the bold move made by Apple, although you can easily spot some similarities between the modern syntax of Objective-C and what Swift uses to impress.

What makes Swift a stronger programming language is the fact that it has adopted all the goodies of other popular languages such as automated memory management, script-like syntax and type inference to create an easier and more streamlined way of working. And more importantly, it is created for the average developer, supporting the development of even the simplest apps.

Why Swift and not old Objective-C?

As stated above, Swift comes with the great advantage of being easier to understand and getting the hang of for the average developer. While coding for Apple"s mobile devices is enough of an incentive, removing the barriers of the programming language creates much more momentum for the adoption of the platform. With Swift, you can pick up on developing for Apple"s gadgets faster, and using "Playgrounds", you can basically teach yourself how to code.

For the seasoned Objective-C developer, there needs to be a good reason for changing. There have been other languages created which did not get the wanted traction, although they were backed by large companies, such as Go, from Google. But while with Go "there is no real incentive to use it", according to Paul Jansen, an independent code mindshare tracker, Swift comes with real improvements over the current way of working, this making it desirable for all the developers using Objective-C.

On it"s own, Swift is very similar to other languages available out there, but considering that it"s only alternative is Objective-C, many of the developers will choose Swift for it"s easier way of working. It comes packed with features such as "inferred typing" which basically means coders do not have to specify types for the used variables, while at the same time the code is safer, since the variable gets the type of it"s first value.

Probably one of the most important changes made to the tool used to develop iOS applications, Xcode, is the addition of "Playgrounds". This is a place where you can see in real time what the code you have written will yield, thus making it faster to test ideas and practically teach yourself by trial and error how to code in Swift. Giving it some more thought, "Playgrounds" go to witness more than the ability to test a few lines of code. What it basically demonstrates, is the fact that Swift is extremely fast, since it has to compile the code, run the executable, and show the result. All of this is happening in a matter of under 2 seconds, depending on the code size and complexity, meaning that an applications developed with Swift will be very fast on the devices it"s run on.

And it doesn"t stop there. You can alter a running program without having re-build it. Playgrounds will basically inject the code into the running application, thus making sure it is up-to-date to the current implementation. Neat!

Is Objective-C going to disappear?

The best part is that Apple isn"t taking away support for the old language and applications can actually have code bases that contain both Swift and Objective-C. And for good reason!

While Swift seems easier to learn and work with, the verbose style of Objective-C gives more context to a newcomer. This can be particularly important when a project is inherited by a new developer. Since Swift gives more and more ways to diminish the amount of code written, it becomes more and more cryptic. So the need of comments can become mandatory.

Apple has thought of all the situations where Objective-C might still be needed and is even allowing the use of both Swift and Objective-C in the same project. This should give the option to the developer to use a library that was written in Objective-C, or reuse a piece of code if it makes sense to not rewrite it just yet.

It"s not all about performance and efficiency

As I was saying earlier, Swift has a great advantage over other new programming languages when it comes to being adopted by the current developers. It delivers on performance and also on ease of use.

But is that the only target for Apple? Only to make it"s platform work better? From where I"m standing, it looks like a marketing stunt as well. Objective-C was considered hard to learn and to adapt to, especially by seasoned developers of other technologies. And thus came Appcelerator Titanium, Xamarin and the likes. Being that now, Swift resembles many of the languages used broadly in the industry, the switch to the Apple platform is easier and guarantees the best results, unlike using one of the aforementioned frameworks.

Taking for a case study the Web developers - the culprits are mobile web pages and Appcelerator Titanium. At the date of writing this article, according to Appcelerator"s homepage, there are no less than 618.722 developers using the platform. That is quite a bunch. Wouldn"t Apple want to claim some of that to their own development platform? This would make sure that the developed apps are more stable (no middleman) and faster, more components are created for Swift, and chances are more revenue will be generated inside the ecosystem as purchases and payments can be done through the AppStore, instead of the web.

If we compare Swift with JavaScript, we can easily see the resemblance in the way the code is written:

JavaScript:
var country = "Argentina"
var countries = ["Argentina", "Brasil", "Mexic"]

Swift:
var country = "Argentina"
var countries = ["Argentina", "Brasil", "Mexic"]

As you can see the syntax is similar for these examples, although, considering the "inferred type" feature of Swift, the behavior of the variables is a bit different.

Swift has borrowed many features from other programming languages, making it a mix of great ideas. You can see similarities with Javascript, Python, Java, C#, C, Lisp, Cold Fusion, JSP and a few others.

Using Swift

To exemplify the use of Swift, we"ll create a tip calculator application.

class Calculator {
 
  let total: Double
  let taxPct: Double
  let subtotal: Double
 
  init(total:Double, taxPct:Double) {
    self.total = total
    self.taxPct = taxPct
    subtotal = total / (taxPct + 1)
  }

  func calculate() {
    println("10% tip = : (subtotal * 0.10), 
		for total = (total)")
  }
 
}

let tipCalc = TipCalculator(total: 10, taxPct: 0.24)
tipCalc.calculate()

Conclusions

Considering what it replaces, Swift is much better at allowing fast development and great performance. With these advantages, it"ll make its way into the hearts of even the most seasoned Objective-C developers, and most probably at a faster pace than other languages.

Do you need to care that it still is a programming language specific to the Apple ecosystem? It depends on what your focus is. Apple has stopped supporting Java a while back, so a fusion with the programming language that is powering Android is not in the books anytime soon. With this in mind, a revolution within the platform is most welcomed and will probably power more innovations in the near future. It may even be the case that it will be open-sourced, as Apple has done with Clang and LLVM, giving the masses the opportunity to contribute.

Conference

Sponsors

  • comply advantage
  • ntt data
  • 3PillarGlobal
  • Betfair
  • Telenav
  • Accenture
  • Siemens
  • Bosch
  • FlowTraders
  • MHP
  • Connatix
  • UIPatj
  • MetroSystems
  • Globant
  • Colors in projects