Learning Swift and XCode at the same time

Joe McMahon
5 min readJun 25, 2015

--

This post describes my experience learning Swift and XCode with no previous knowledge of them. And I have never written a single line of Objective-C before.

Over the last year I have attended a few local meetups and sat in on a single class. But mostly, I have been teaching myself by making a game in Swift — Orbert. I also looked at a lot of other people’s Swift code on GitHub and the blogosphere.

I hope this is useful for people who have none or some programming background and are thinking about making the jump into iOS development. I hope to convince this type of individual that learning Swift is a good bet to make.

The Story

Apple announced its new language called Swift last year at WWDC 2014. Swift compiles to the same assembly language as Apple’s other language — Objective-C. Because they both compile to this common assembly language called LLVM, they can easily operate in combination together.

So when I saw this announcement, I decided to pay $100 bucks and sign up for an Apple developer’s license. Now, I have been programming for some time. I first learned Dr Scheme in high school, which is a lot like LISP — a functional programming language. Then I went on to web programming with stuff like JavaScript, PHP, and Python — I'm still not an expert on these but I can get by. I have also dabbled with C, embedded programming, and all that nasty memory management that goes on. Throw in stuff like Git, MatLab, and Mathematica and it is safe to say I have a decent programming background.

Step 1

My first step of learning was to watch Apple’s WWDC 2014 videos. Then I downloaded some Flappy Bird swift code off Github and took a look at it.

What immediately struck me as how little code there was. Here was this immensely popular game, yet it had hardly any code. Just 267 lines!

My First Framework (aka: Library)

I noticed a single line of code at the top of Flappy Bird.

import SpriteKit

What is this? It is a native Apple framework (also called a library).

SpriteKit is a graphics rendering and animation infrastructure that you can use to animate arbitrary textured images, otherwise known as sprites. SpriteKit provides a traditional rendering loop that alternates between determining the contents of and rendering frames.

Making games is great programming experience. So I decided that I would ship a video game and SpriteKit seemed like the best library to use. Thus I spent the next six months learning everything I could about it.

Struggling with the Learning Curve

You'll notice in the Flappy Bird code, there is a SKSpriteNode declared.

var bird:SKSpriteNode!

It is clear that the variable “bird” is defined as a SKSpriteNode. But notice the exclamation point at the end. What the hell is that?

Swift doesn't quite look like any other programming language. It resembles Python in its elegant minimalism, but the structure of objects reminds me of C/C++/Java. But then it can also become very functional and have a ton of parentheses like LISP. Oy vey!

Throw in XCode, with its multiple side panels and deep menu system, and it is easy to get overwhelmed.

The best way to deal with all this complexity is to implement one small feature at a time or to refactor one bit of code at a time. This way, you are not forced to know absolutely everything at once, and are still actually coding.

Try creating a new default project and then hitting XCode’s “play” button and see what happens. It really is fantastic that a new user can just open up a project and deploy it with minimal knowledge.

The Docs

Apple did a great job with documentation. Use it! For example, I had to fully understand the difference between a SKNode and a SKSpriteNode. By looking at the documentation I could learn that a SKSpriteNode is a subclass of SKNode. That means any function I can call on a SKNode can also be called on a SKSpriteNode.

There are all sorts of weird syntax in Swift. Be sure to spend time reading the docs about these commands, even if you aren't using them. Chances are you will end up using them when you refactor your code.

The Community

Engineering efforts are only as good as the community they take place in. So I recommend reading blog posts. Start with the Artsy blog and perhaps the Brooklyn Swift Code of Conduct. Basically, it’s getting there. A lot of people are working hard to make sure it does, but any programming language needs time for its community to develop. After a while, the community will fill the internet and Stack Overflow with questions and answers all about Swift.

Apple has shown an astounding amount of support to the early Swift community. So it is really safe to bet that Swift will replace Objective-C. Now, if only Android devices could compile Swift…

The Problem

Objective-C.

Most iOS programmers are still using it. People don't think an app can be built purely in Swift — and for the most part — they are right. However, a lot of apps are starting to be written entirely in Swift. Many people probably still have a couple of lines of Objective-C in there somewhere. Also be ready to read some Objective-C and have no idea how to write it in Swift.

So when I came on the scene, there were already all these other iOS programmers talking about tools, libraries, and frameworks that I knew nothing about. Namely I had to learn about CocoaPods, a dependency manager for iOS libraries.

Objective-C community
+
New iOS language
=
Easy to leverage existing infrastructure and networks

You are not just learning XCode and Swift, but you are also learning about the history of iOS development and the existing community behind it all.

Closing Remarks

If you are a new or experienced programmer considering iOS development, I highly recommend learning Swift first. Don't bother with any Objective-C, just jump right into Swift.

Pick a simple project to work on that does not involve a web server. Get into networking later on once you understand how the language works.

My best word of advice for climbing the learning curve — refactor your code often. If you are trying to ship something, this will still help keep your code clean, even if it slows you down in the short run. For example, I had been custom writing code for the gravity between my character’s spaceship and all the planets in the level. But then I found that SpriteKit has a built in class called SKFieldNode and I did not need to implement any physics algorithm at all! So by refactoring my code, I made gravity work better and I could tinker with it in faster iterations.

--

--