Diving into SWIFT
Tags: swift, ios, beginners
Published: 2019-12-14 21:08:57

After spending the last month working on a native application in React Native, I figured it would be interesting to dive deeper into mobile development and learn a little bit more about Apple's very own programming language: **Swift**.  In this post, I will explore why **Swift** has swiftly (sorry, had to do that once) become the most popular language for iOS development and dive into some of the basics including:
 - Variables
 - Collections
 - Functions

# What is Swift?

**Swift** is a programming language for the suite of apple devices first released in 2014.  It is meant to be a higher-level, more modern take on Objective-C and has certainly lived up to expectations by becoming the most popular language for iOS development by 2018.  It is open source and has a comprehensive, 500+ page manual available for free download [here](https://books.apple.com/gb/book/the-swift-programming-language-swift-5-1/id881256329).

# Why learn Swift?

Well, if you like Apple products (I shamelessly do), Swift is the way to go.  As previously mentioned, it is currently the most popular programming language across apple devices and its popularity is continuing to rise.  Check out **Swift's** position on the 2019 developers survey from stack overflow:
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/mfzlxyr2nk8g03cpszdt.png)
If you are hunting for jobs, **Swift** isn't so bad either.  A quick search on [ZipRecruiter](https://www.ziprecruiter.com/Jobs/Swift-Programming) currently shows **3336** open positions posted across the country (as of today, Dec. 14, 2019) posted within the last 30 days.

Lastly, **Swift** is a language that isn't too hard to pick up if you are already familiar with another language or two (I am coming from a JavaScript and Python background).  Let's see some **Swift** basics:

## Variables in Swift
####Running example code: for simplicity, I recommend trying out [repl.it](https://repl.it/languages/swift)'s **Swift compiler**

Variables can be declared with either the `var` or `let` keyword.  If the value of the variable is going to be reassigned, `var` should be used. 
 Constants (variables that will only be assigned once) should use the `let` keyword (like `const` in Javascript). Variables *must* be declared before use!
```swift
var changeMe = 10
let dontChangeMe = 99
```
If we want to log values to the console, we can use the built in `print` function.
```swift
print(changeMe)
// ==> 10
```
// are used for inline comments and we do not need to use ; to end expressions! Nice.

Most of the time, we may want to specify type when defining variables.  This can be really helpful to catch bugs as your code base grows.  To specify type, we add a `:` followed by the datatype.  An error will be thrown if these variables are not assigned to the specified datatype later during code execution.
```swift
// explicit variable declaration and assignment
var firstName: String = "Avery"
```
Reassigning `firstName` to another data type later will throw an error.

## Collections
**Swift** keeps it simple, with three major types of collections: `Arrays`, `Sets`, and `Dictionaries`.
 - **Arrays** are indexed collections of data, comma separated within `[]`.  They can be made immutable (into a tuple) simply be declaring them with the `let` keyword.
```swift
var mutableArray: [Int] = [1, 2, 3, 4]

let tuple: [String] = ["Can't", "Change", "Me"]
```
Notice we can explicitly set the type of both the Array and its contents after the colon.
 - **Sets** are unindexed collections.  They look really similar to arrays, except if iterated over, the order will be random.  These are good for storing data where order doesn't matter.
```swift
var myCats: Set<String> = ["Frank", "Hodor", "Buddy"]
```
 - **Dictionaries** are collections of key-value pairs.  They also look pretty familiar to sets and arrays.
```swift
var me: [String: String] = ["Name": "Avery", "Age": "32", "City": "New Orleans"]
```
Unlike with javascript objects, we can use the data type of our choice for our keys!

## Functions
We define functions in Swift with the `func` keyword.  We must also make some decisions about what datatypes our arguments will be, as well as if our function should return a value.
####Functions with no return value
```swift
func sayHello (name: String) {
  print("hello, \(name)")
}

sayHello(name: "Avery")

// ==> "Hello, Avery"
```
So a few things here:
  - We name our function `sayHello` and specify that it is expecting a variable with a type of `String` when it is invoked.  Our print statement shows of **Swift string interpolation** as `\(name)` will be replaced by the function's argument when it is invoked hence the log of `Hello, Avery`, if you run this code in your compiler.  
####Functions with return value
```swift
func sayHello (name: String) -> String {
  return "hello, \(name)"
}

print(sayHello(name: "Avery"))
```
Notice we add a `->` (arrow) and specify that our return value will be of data type `String`

I hope you have enjoyed this brief intro into **Swift**.  Clearly, I have just scratched the surface for what **Swift** is and can do.  For more, I encourage you to check out the official docs [here](https://docs.swift.org/swift-book/LanguageGuide/TheBasics.html).