Swiftly picking up iOS Development

From a few JavaScript frameworks, Java and a little bit of Android programming to Swift…

I’ve technically written in a number of programming languages thanks to numerous CS courses that insisted I be exposed to the nuances of different code, but I’ve spent most of the time as a professional developer writing in JavaScript and/or Java.

Instead of stubbornly bumping over the potholes on the road to swift proficiency, I decided I’d fill in the gaps of my knowledge with a little swift primer.  Continue reading to find my cheat sheet of takeaways for the OOP (but not yet swift) programmer.


Constants & Not

Variables in swift (like in Java) come in immutable and mutable varieties specified with the following keywords:

  • constant (immutable) use the keyword let
    let pi = 3.14
  • variable (mutable) use keyword var
    var nickname = "Ash Attack"

Swift can use type inference to infer the type based on the initial value, but unlike the loosely typed free for all that is Javascript in swift once a type is set it cannot be changed.

If you want to specify a type, you write it after the variable name separated by a colon:

let pi : Double

Optionals are variables that could have a value or could be nil (null is out we’ll have to start saying “nil” now).  To use an optional, you have to unwrap it.  To implicitly unwrap an optional, you use “!” which is fine as long as you know the optional will always have a value after initialization (e.g. outlets connecting the UI & source code).

Flow Control

If Where?

The first time I saw this syntax I felt that the programming language and mySQL had a baby.  Let’s say I really only like puppies whose names begin with I:

var puppy:String? = "Ivy"
if let pupNameTag = puppy where puppy!.hasPrefix("I") {
   greeting = "Hello \(pupNameTag)"

If puppy doesn’t have the prefix “I”, the variable pupNameTag is nil and the if statement fails to execute the code block and the people-for-puppy-egos descends upon me as I have become some rude person who doesn’t say hi to the adorable puppy.

The code above also shows the format for String Interpolation.  I found the following behavior a bit strange: if I want to print an optional’s value with the String Interpolation format the output of “String Interpolations with Optional \(optVal)” where var OptVal:String? = “bob”  (notice the “?” declaring this an optional) prints out

String Interpolations with Optional(bob)

I’m sure that’s going to surprise me someday.

Range Operator

A couple new operators for me

The half-open range operator:


for i in 0..<4 { }

Essentially the following in Java

for (int i = 0; i<4; i++)

And the close range operator (aka inclusive)

for i in 0...4

Essentially the following in Java

for (int i = 0; i<=4; i++)

And apparently, the swift folks have decided that there’s no reason for there to even be an ‘i’ if we don’t need it in the loop (I do teach intro to programming classes and you’d be surprised how many people have a thing against the ‘i’).  They have provided us with an underscore as a wild card so this works:

var count:int = 0
for _ in 0...4 {

And when we’re done, the value of count is 5.

Functions & Methods

The syntax notes:

  1. The keyword func declares a function
  2. name:type is the parameters list format
  3. -> ReturnType 
func aFunction(name:string)->Int { 
   // Do Stuff

A special note (which in hindsight made the first tutorial I went through make so much more sense): When calling a function you list the first parameter without its name and every other one in the format name:value

Lingo Alert:

Functions defined within a specific type are called methods and are called with the dot syntax

Classes, Initializers, Inheritance

Initializers Aka “Constructors”

Because we can’t keep it uniform to what we just learned about Functions:
When you call an initializer all parameters are in the name:value format

There are a few different terms and types of initializers in Swift:

failable initializer – An initializer that can return nil after initialization if the parameter values that are supplied are tested and out of a desired scope

Designated Initializer – A “primary initializer” any initialization chain must ultimately call a designated initailizer.

Convenience Initializer – Most likely will call on a designated initializer supplying default values for parameters not required when calling the convenience initializer itself but still needed to initialize an instance of the object.

Class & Inheritance

Required – A keyword that’s used to declare every subclass with the “required” initializer must implement its own version

Classes are declared with the following syntax:

class SubClass:SuperClass {
   // Be cool

Similarly to that @Override notation in Java, Swift has an override keyword that you can use when overriding a superclass function

override func name()->returnType

Enums & Structs

We have enums in Java and I’m familiar with Structs from the ancient times when I wrote C code, but I noted the syntax for declaration:

enum Name:rawValueType

Unlike C enums in swift each case in the enum does NOT require an correlating int value.  However, if you do have a raw value you can declare so as shown above.

Note: Structs in Swift are similar to classes.  They have methods and initializers but are passed by copying while classes are passed by reference.


Basically an Interface

Lingo Alert:

Protocols are adopted by a class, struct or enum.

Any class, struct, or enum that “implements” a protocol is said to conform to the protocol.

get – a keyword to specify that a property is read only:

var readMe:String { get }

To declare you are conforming to a protocol list it after any super classes in the class declaration:

class SubClass:SuperClass Protocol {
   // I implement all the protocols


I suspect typing this up will be most helpful for: me, and after writing a bit more code, I won’t even be able to remember a time when this wasn’t second nature.  But on the off chance this serves as a launch pad for other JS/Java Devs wanting to swiftly dive in to iOS development without having to sift through programming basics to find the gold nuggets that’ll make that swift code blurb you’re staring at look like plain text– Enjoy!


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s