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 Dr. Angela Yu's Udemy course on iOS13 & Swift 5 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:
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 Optional Binding. Conditional Unwrapping allows you to unwrap your optional property with a if let 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:
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. ๐ Swift Programming Docs - Learn more about Swift Closures.
3. Extensions ๐จโ๐งโ๐ฆ
Extensions are used to add new functionality to an existing class, structure, enumeration, or protocol type. Here's an example:
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.
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! ๐ค"
Swift Programming Docs - Learn more about Swift Extensions
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 here.
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. ๐
Top comments (0)