Learning Swift: An iOS Newbie

We build professional apps for Android, iOS and mobile web.
Have a look

During my initial exposure to Objective-C, I found its unique syntax can take a little while to adapt to. Consequently, it can be increasingly difficult as a fresh iOS developer.

Swift's basics are easy to pick up as the syntax is more similar to popular languages such as Java, Python and C#. It has some interesting features including optionals, optional chaining, generics and simpler values (not a new concept(s) to various languages including Java, of course!). These additions have made iOS development in Swift an easier, safer and neater experience.

Type Inference

There are only two types of ways to declare values in Swift:

let constant = 2 var variable = 2

Swift uses type inference to assign the values a type - it can also be specified if necessary.

var explicitFloat: Float = 1.02

Optionals?

Optionals are values that may contain a value or be nil. Declare an optional value in Swift by placing a question mark (?) after the type:

let optionalInt : Int? = 3

Swift provides safe ways for checking that these values contain anything before doing anything with them. This is done by using guard let or if let. Alternatively, these values can be forced out with an exclamation mark (!).

let definitelyNotEmpty = randomValue!

This is a great article on optionals and their use, if you want to know more.

Generics, protocols, structs..

Swift also supports the use of generics and protocols which are used abundantly here at jTribe. The syntax for a generic function is simple and likely familiar to an experienced developer. An example:

func anyObject<T>(value: T) {}

Protocols are like so:

protocol SomeProtocol {}

And a similar format for structs:

struct SomeStruct {}

A great article on generics and their benefits can be found here.

..And some resources to get you started

The Swift Programming Language book by Apple is my personal primary resource to brush up on any features in the language; useful for developers with any amount of experience. Ray Wenderlich's tutorials are always thorough and clear too, if you prefer the more hands-on approach to learning a new language.

Swift's syntactic sugar makes the language easy to learn, quick to use, and clear to developers (especially important when working in large teams). And now it's open source!