Heads or Tails – Using and understanding Swift

woolIt would be nearly a year since Swift was announced by Apple. It soon became the language of choice for many developers. It is one of the fastest adopted programming language till date. Not only that, it also has one of the largest repository of source code using Swift. Here’s a look at both sides of Swift.

The First Contact

When Swift was announced, it was the miracle cure, the silver bullet – you could now make apps for iOS and Mac OS X using an official Apple Language other than Objective-C. Like an adventurer exploring the unchartered areas of unchartered land masses, overnight there were literally hundreds of blogs and sites that helped others understand Swift.
The first thing that many did was try to map 1:1 Objective-C code with Swift. So you can find most of the pre Swift 1.0 code relating to translating Objective-C to Swift.

The Second Coming

A whole lot of books and code was released using Swift 0.8 and Swift 1.0 including mine. Then when Swift 1.2 was released this year it changed how things worked. Most of the code would not work and the reason was the evolving API signatures. In addition it was also the fact that the way optionals worked changed. At first you could cast variables using as , then it added as? and as currently you can use as!. It was clear that Swift is an evolving language and not even a year old, but has changed the maximum.

Using Swift

Most of the articles on using Swift focused on the new features of Swift as soon as they were released by Apple. The simplicity of Swift provided many developers a chance to jump onto making apps for the Apple ecosystem. A lot of web developers, PHP developers, .Net developers and Java developers found Swift to be THE answer for Apple development. Yet another set of developers, academicians and hobbyists that were poking at Swift to see what it could were playing with Functional Programming. At WWDC 2015, Apple announced that Swift would be Opened Source soon, this sparked even more interest in Swift from the community at large.

The Two sides of Swift

There are two things to consider when working with Swift.
1. Objective-C Runtime bindings
2. The Standard Library Swift syntax and language

While most are focusing on the Objective-C bindings for the articles and blogs, etc, there should be more focus on the Swift language side of things.

Swift has the ability for reflections. This is not fully supported but offers a way to examine the name and values via the mirror struct.

Using Swift 1.2

struct Rock {
 var name:String = "Stone"
}

var rocky = Rock()
var shinyStone = reflect(rocky)

for i in 0..<mirror.count {
  var m = mirror[Int(i)]
  println("\(m.0), \(m.1)")
}

Using Swift 2.0

struct Rock {
 var name:String = "Stone"
}

var rocky = Rock()
var shinyStone = Mirror(reflecting:rocky)

for i in shinyStone.children {
  print("\(i.0) = \(i.1)")
}

This is the pure swift library method, this is also limited in the information that it provides. It outlines the properties of the class and not the functions.

To get a list of the attributes/properties and functions, you will have to dig deeper into the ObjC Runtime which then includes the use of pointers and conversion of pointers to strings and signatures, etc.

Summary

I am equally excited about Swift and believe that Swift is changing the development paradigm. I am specially impressed with Enums , Protocols and Extensions . While these are available with other programming languages as well it is made popular and common with Swift. I have also used Swift to create some iOS applications, authored two books (More iPhone Development with Swift) http://www.apress.com/9781484204498 and (Xcode 6 Essentials) https://www.packtpub.com/application-development/xcode-6-essentials, articles, blogs. However after nearly a year, I feel slightly different about Swift. Specially when it will be available as Open Source soon.
Firstly, I believe that there should be more focus on using pure Swift than relying on Objective-C libraries alone if you want to later create cross-platform applications.
The other side of Swift is to create Swift bindings with the framework/classes of your choice.

You can use Swift to create your mobile applications or Apple Watch applications or use it via playgrounds for academic purposes, which ever works for you. Objective-C is and will always be closely interrelated with Objective-C where as Open Source Swift would be like C++ would require bindings to make them work.

There is however one major risk with the future use of Swift by a lot of new developers. While on one hand Swift is easier to work with, on the other hand a majority of the articles and blogs run the risk of scaring new developers away. It can so happen that despite Swift seeming like an easy to use language can become complicated and a deterrent to start development like Objective-C. A proper implementation of Swift and advance use of Enums and Protocols can make developing with Swift a breeze. Looking forward to more articles that focus on the simplicity and easy to use Swift features.

Views All Time
Views All Time
1327
Views Today
Views Today
1
Posted in Basics and tagged , , , , .