Swift - 1 Month.
Tags: swift, ios, apple, xcode
Published: 2020-11-10 12:35:12

# Introduction

Recently I've been learning Swift & iOS development for the past month now and I've decided to document my journey, every month I'll write a post about everything I've learned, the challenges I face, and the goals I set for myself. 😄

### What I've learned

So I've been following <a href="https://www.udemy.com/course/ios-13-app-development-bootcamp/">Dr. Angela Yu's Udemy course on iOS13 & Swift 5</a> and so far I'm pretty impressed with the course and I've already learned so much about the Swift programming language, I definitely recommend it if you're looking to learn Swift. So here are some of the interesting things I've learned:

--------------------------------------------------------------

#### 1. Optionals 🤷‍♂️
Swift was designed to be a safe programming language, which means it's less likely to get runtime errors. A common way that code can fail is when it tries to use data or a value that is missing. For example:

```Swift
struct Human {
    var firstName: String
    var middleName: String?
    var lastName: String

    func fullName() -> String {
        if let middle = middleName {
            return "\(firstName) \(middle) \(lastName)"
        } else {
            return "\(firstName) \(lastName)"
        }
    }
}

let human = Human(
    firstName: "Mary",
    middleName: nil,
    lastName: "Smith"
)
```

Here I created a ``struct`` which has three properties, ``firstName``, ``middleName`` (which has a type of optional string) and a ``lastName``. When a property has an optional type, it means that the property can either have a value or it can be ``nil`` so no value.

In the code snippet above, I unwrapped the ``middleName`` property so the program wouldn't crash, this is called <a href="https://medium.com/@strawb3rryx7/swift-what-is-the-optional-binding-7624c7f9098e">Optional Binding</a>. Conditional Unwrapping allows you to unwrap your optional property with a <b>if let</b> statement, this means if the property is not nil, it will be unwrapped into the created constant ``middle`` then you can use this constant in the if let statement block.

--------------------------------------------------------------

#### 2. Closures 🚪
Closures are functions that you can pass around in your code. And yes when I first came across closures in Swift I was completed lost 🥴 but trust me, it's easier than it looks, here's an example:

```Swift
func add(num1: Int, num2: Int) -> Int {
    return num1 + num2
}

func area(w: (Int, Int) -> Int, h: Int) -> Int {
  return w(5, 10) * h
}

let result = area(w: add, h: 2)
print(result) // Prints 30
```

We defined a ``add`` function that takes two parameters, and returns the result when they're added together, simple enough right? 😅

We define another function called ``area`` which takes in two parameters, the first parameter is a function that is expected to have 2 parameters which are integers and returns an integer. In the function body, we call the ``w`` parameter which also calls the ``add`` function because we pass the ``add`` function as an argument when we called the ``area(w: (Int, Int) -> Int, h: Int) -> Int`` function. The result from the ``add`` function gets stored in the ``w`` parameter and then the ``h`` parameter is added on. I hope that wasn't too confusing, I'll probably write an in-depth blog about closures sometime soon. 😄 <a href="https://docs.swift.org/swift-book/LanguageGuide/Closures.html">Swift Programming Docs - Learn more about Swift Closures.</a>

--------------------------------------------------------------

#### 3. Extensions 👨‍👧‍👦 

Extensions are used to add new functionality to an existing class, structure, enumeration, or protocol type. Here's an example:

```Swift
extension String {
    func appendLastName(_ lastName: String) -> String {
        return self + " " + lastName
    }
}

let name = "James"
let fullName = name.appendLastName("Bond")

print(fullName) // Prints "James Bond"
```

In the example above, we defined an ``extension`` the extends the ``String`` struct capabilities. We defined a method called ``appendLastName`` which takes in a parameter named ``lastName`` and we append it to the current string, which is ``"James"``. Not too hard, right? 🙃

Think of extensions as learning a new skill set, when you've learned to code you essentially gain a new skill set, that skill set being that now you know how to code. Here's an example to demonstrate this scenario a little bit better.

```Swift
struct Nicholas {
    func canRead() {
        print("I can read! 😄")
    }
}

extension Nicholas {
    func canCode() {
        print("I can hack your facebook! 🤖")
    }
}

let nicholas = Nicholas()
nicholas.canCode() // Prints "I can hack your facebook! 🤖"
```

<a href="https://docs.swift.org/swift-book/LanguageGuide/Extensions.html">Swift Programming Docs - Learn more about Swift Extensions</a>

--------------------------------------------------------------

#### Challenges I faced 😓

Something I didn't understand quite well were Enums. According to the Swift docs, enums are "a common type of group related values and enables you to work with those values in a type-safe way within your code" I'll probably have to work and practice more with Enums to gain a better understanding of them. You can learn more about Enums <a href="https://docs.swift.org/swift-book/LanguageGuide/Enumerations.html">here.</a>

--------------------------------------------------------------

#### Goals for next month

Now that I have a solid understanding of Swift syntax, It's time to dive into making apps and applying all of my knowledge. I hope to gain a good understanding of how iOS apps are made by the end of the second month, I'll see there! 😉


--------------------------------------------------------------


#### Note ⚠️
I was supposed to publish this blog a couple of weeks ago but didn't make time because of applying to universities and school work, I'll be sure to publish the next blog on time. 🙂