For the longest time, I’ve been interested in tech. I’ve been learning about it since the first iPhone came out in 2007, and one thing I’ve never bothered with was learning the code that made these machines work. There’s no doubt I wanted to do it, but it seemed so daunting — like it would use up a lot of my time just going at something I was unsure of. That’s what I was thinking for almost 9 years. Soon, coding started sparking my interest's a little more. So I decided I was going to start learning. No excuses, no stopping, just learning.

As I searched the web for places to learn to code, I noticed that it was VERY tough to find a reliable website to teach me to code — in fact, I didn’t find a website. All I wanted was a website that was free, easy, and simple to understand and provided examples to work with. I never did.

Instead, I found an app called Swifty. Swifty is free on iOS. So I decided to give it a try. The app was beyond capable in satisfying my needs and even provided interactive examples to work with.

The only problem with the app is that it tries to make every step bite sized. It’s not bad at all when learning only the very basics of code, but when the examples get more complex, the bite sized snippets just make learning more confusing. After you complete the first few levels it will tell you you need to pay to get more lessons. I recommend you don’t — that’s where the lessons get confusing.

You might be asking yourself “So, where do I learn more?”. The next step is to read Apples guide to Swift 2.2. Although it’s less directed towards complete beginners, it gets the job done better than anything else. It is available on iBooks for free.

With that out of the way, you probably came here to learn the basics of Swift. So I’ll teach you the basics. I am also learning, so putting my thoughts out in words causes me to think and helps me remember and, understand things I may not remember — it’s like a reteach for me. It’s a win-win for both of us.

Blah Blah Blah. Lets Start Coding.

Okay, but before you start, you’re going to need some resources. If you’re on a Mac and you already have Xcode installed, you can use that. If you don’t already have it installed, don’t get it yet. Use this website by IBM. For Windows users, you can use this site too.

And while you’re at it, grab Apples guide to Swift 2.2 on iBooks.

Lets Start

okay, here’s the good stuff. This is where we start coding. First open the website linked above. The website will have two boxes, one on the left, and one on the right. The one on the left is the box we write our code in. Right now, if you click the little play button on the bottom in between the two boxes, it should print “Hello World!” to the console (the box on the right).

Input: print(“Hello World!”) Output: Hello World!

If you want to play around and get familiar with the print function, try changing the words in between the quotes and see what output you get in the console. If you’re getting any errors, make sure you didn’t remove the parenthesis or quotes from the line and you didn’t add any spaces.

The print function is used throughout Swift quite often, so it’s good to get familiar with it before getting into more complex code.

Constants and Variables

Now we’re going to learn about constants and variables. These provide the program with information that can be either permanently stored or changed later on.

What is a variable: In Swift, a variable is labeled as var. So if I wanted to set a value that constantly changed throughout my program or something that is logged over time, I would use a variable — so I could change the value later on. Here is an example:

var hairLength = (“Five Inches”)

print(hairLength) hairLength = (“Six Inches”)

print(hairLength)

Sally wanted to track the growth of her hair using code. The code shows that her has grown one inch. And because hair grows, that means the variable needs to change later in the code to reflect the change.

To print the results to the console, you need to add a print command to the code. To do this just type print(yourVariableNameHere) but this time don’t add quotations around the variable name, because our variable is already set. You should see your updated variable print in the console.

What is a constant: A constant is a value that stays the same throughout. It never changes and nothing can change it. Something like a name. You use a constant by using the command let. It works the same way as a variable, in the sense you can use the print function the same, but a constant can’t be changed like a variable. Here is an example using my name:

let firstName = (“Nathan “)

let lastName = (“Goodwin.”)

let name = (firstName + lastName)

print(name)

Try it with your name too. The result should print your name into the console.

Challenge: Try changing ‘firstName’ later in the code like you’re using a variable. What error do you get?

Think of var and let like boxes. They can both hold things, but in the box labeled “Variable” stuff can be taken in and out, where as in the box labeled “Constant” is glued shut and nothing other than what was originally in the box can be taken in nor out.

//This lesson is over! Just play around with print, let, and var. You’ll eventually get the hang of it. My next article will be soon! Thank you for reading!