My FeedDiscussionsHeadless CMS
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more

Swift! Let's Learn It Together!

Steven Wong's photo
Steven Wong
·Jan 27, 2020

What is Swift?

Swift is a programing language that was developed by Apple Inc. for Apple's devices and Linux. It was first introduced back in 2014 at Apple's Worldwide Developers Conference (WWDC) and has since then received major version changes. For my next couple blog posts relating to Swift, we will be looking at, and going through version 5.1 of Swift which was officially released back in September 2019. Swift take's a lot of it's language ideas from "C, Objective-C, C++" and many others but seeing that I'm coming from a Java background, we will be largely going over the differences between the two so we're able to dive right in for the next blog and start developing a small iOS application!

What Do We Need to Start Coding?

Well to start, the best way to go about coding in Swift for what we will be attempting to do later on down the road will be in a macOS environment. For all my learning's and tutorial's I will be writing about, they will be done through a VM on VMware Workstation. There are many tutorials on the web on how to get started and I encourage you to look those up and get your environment setup for yourself. It's a lot of work, and make sure to follow the instructions closely or else it can be cumbersome.

For our IDE, we will be using Xcode as it has built in native-support. We can get this from the App Store within macOS as long as you're on macOS version 10.14.4 or higher. Xcode is a great environment for us to get familiar in for development and it will be the main tool we use when we get into SwiftUI as well.

Lets Get Started!

For this first blog post, we're mainly going to be going over Swift syntax and getting familiar with it. Some of it is like Java, and some like Javascript which should help us learn at a swifter pace. Yes, pun intended.

Variables and Constants

Like many other languages, variables have to be declared before we can use them. Variables are mutable, and are declared with the keyboard var while constants are immutable and declared with the keyword let.

let constantType: String = "This is a constant declared"
var variableType = "This is a variable declared"

Here we declared the constantType a String, and variableType wasn't explicitly called a String, but Swift is able to understand and makes the variable a String for us. We don't have to declare or define what our constants and variables are, but we can if we'd like to enforce certain rules within our application.

In Swift, we will be using the built-in data types: Int, Float, Double, Bool, String, Character, Optional, and Tuples which as I'm sure you can see, as a Java developer, we should be able to recognize nearly all of these.

Optionals

Optionals was introduced in Swift in version 4. Optionals, basically the gist of them is "there is a value, and it is X" or "there is no value at all."

var optionalTest:Int? = 9

if optionalTest != nil {
    print("optionalTest contains a value!")
} else {
    print("optionalTest contains a nil value!")
}

What do you think the suspected output is here? Well I hope you guessed the first print option!

As you can see from the above sample code, a lot of the structure is like what we're used to seeing in Java, except there are some differences. First, a null is called a nil in Swift, and there are no semi-colons!

Now, with optionals, if we define a variable as optional, then in order to get the value from the variable, we have to unwrap it. How do we do this? Well with Forced Unwrapping we just put an exclamation mark at the end of the variable. Lets work off of the previous code and see how we can achieve this!

var optionalTest:Int? = 9

if optionalTest != nil {
    print("optionalTest contains a value! That value is: \(optionalTest)")
} else {
    print("optionalTest contains a nil value!")
}

If we run this, we get the following result: optionalTest contains a value! That value is: Optional(9)

Now, that's not very pretty or useful when making applications now is it? So how do we fix it? Simple! Change print("optionalTest contains a value! That value is: \(optionalTest)") to print("optionalTest contains a value! That value is: \(optionalTest!)") and that will change our output to the following: optionalTest contains a value! That value is: 9

Collections

Collections in Swift are largely similar to Java and Javascript. This will be a quick gloss over, but if you need more information, Swift's own documentation is great!

Arrays

var arrayOne = ["Khosro", "is", "the", "best", "teacher!"] //explicity defined the contents
var arrayTwo = [String]() //initializes the array.
var arrayThree: [String] = [] //Declaring arrayThree as a String array and initializing it.

Sets

var example = Set<String>()
var example2: Set<String> = ["Arran", "and", "David", "Suck!"]
var example3: Set = ["just", "kidding"]

Dictionaries

Swift dictionaries are written as Dictionary<Key, Value> where the key is the type of value that is used as the key, and value is the type of value that is stored for those keys.

var airports: [String: String] = ["YYC": "Calgary International Airport", "YWG": "Winterpig"]

If we wanted to add to the list of airports, then were would use the following to add something to the airport dictionary.

airports["YVR"] = "Vancouver International Airport"

So Whats Next?

Well, I hope that was a good introduction into Swift for yourself as it was for me. I started with this in my learning's to get a understanding of the syntax, languages and Xcode in order to write code. Instead of jumping off the deep end into the pool, and not knowing how to swim, I'll be taking you and I through the kiddie pool, all the way up into the deep end, learning to swim together on the way out there. That is how I envision learning Swift, and hopefully by April we will be able to have built a small iOS application together that we can potentially even load onto our iPhone's (sorry other devices!) and can interact and play with! As for the next blog, we will go a little more in-depth on the programming side of things, and potentially be making the jump into incorporating SwiftUI into it as well!