10 Tips to become a better Swift Ninja🏴
This isn’t a part 2. You are now in a different league
I’m back. I promised with you guys that if I receive 100 likes on my previous post. I would make a part 2… Well, let’s take a look.
Like legit. How did it happen so quickly? What’s wrong with this. Last night, I knew I had to get it going.
First, I have to admit how distracted I had been for the past days and hours because of you guys… I have shit tons of Swift tutorials to make, but not sure if I can finish them on time. 😅 Oren Alalouf, I am trying my best bro… please forgive me if I miss it.
You can check here for the uploading schedule on YouTube
Second, I’ve been talking too much.
Talk to you once I meet the purpose of this article first. Let’s dive in.👊
Again, please excuse me for the formatting. I tried to have it as concise as possible. Feel free to play around with Playground, and if you spot any problems, please comment below. I will fix it/them asap.
1. Convenient Initializers
Ex) Get a number of toes and fingers for a human
// Original Way
class Human {
var finger: Int
var toe: Intinit(finger: Int, toe: Int) {
self.finger = finger
self.toe = toe } }var elon = Human(finger: 10, toe: 10)
elon.finger // 10
elon.toe // 10
Most humans have 10 fingers and toes. Don’t you input, pre-initialize.
// Cooler Way
class Human {
var finger: Int
var toe: Intinit(finger: Int, toe: Int) {
self.finger = finger
self.toe = toe }convenience init() {
self.init(finger: 10, toe: 10) // referring to the top init block
} }var bill = Human()
bill.finger // 10
bill.toe // 10
Convenience init Lesson: Tomorrow 8am
2. Type Casting
Ex) Merge [Int] and [String] array into [Any]
When you make an API call from Facebook, you would receive a series of urls of profile pictures[String] and the number of likes[Int] for each.
You want to merge those two different types of arrays into a single array just for the sake of portability.
Remember, I explained the logic in the previous post.
fewer variables → fewer life issues (shorter version)
// Upcasting (as)
var likes = [123, 342, 231] as [Any]
var photos = ["Beach", "Girls", "Chill"] as [Any]// merge them
for like in likes {
photos.append(like) } photos // ["Beach", "Girls", "Chill", 123, 342, 231]
You’ve upcasted the both arrays into Any. You’ve then merged them. As a result, you now carry everything in a single array, called, “photos”. That’s pretty handy, isn’t it?
Now, it’s time to downcast. In other words, time to convert from [Any] → [String] or [Int]
// Downcasting (as?)for i in photos {
if let number = i as? Int {
print(number) } // 123, 342, 231
if let string = i as? String {
print(string) } // "Beach", "Girls", "Chill"
}
3. Switch Statement vs If-Else
Ex) Recommend healthy drinks for different age groups
Age Group
1-7: Milk
8-80: Soda
81-150: Water
Beyond: ??
// Aweful Code
var myAge = 20if myAge >= 1 && myAge <= 7 {
print("🍼")
} else if myAge >= 8 && myAge <= 80 {
print("🍺")
} else if myAge >= 81 && myAge <= 150 {
print("🚿")
} else {
print("You alive, bruh?") }
Stop repeating.
By the way, just to show some respect, I’ve researched and found the oldest person ever lived was 122 years 164 days according to Wikipedia
Anyway, stay focused.
// Beautiful Code
switch myAge {
case 2...7: print("🍼")
case 8...80: print("🍺") // X find the right emoji
case 81...150: print("🚿")
default: print("You alive, bruh?") }
“Sweet, I love 🍺”— Bob
4. Function Custom Parameters
Ex) Print a traveller’s direction
Prints a traveller’s origin and destination
func getD(userOrgin: String, userDestination: String) {
print("From \(userOrgin), to: \(userDestination)")}getD(userOrgin: "🇺🇸", userDestination: "🇰🇷") // From 🇺🇸 to: 🇰🇷
What if the user doesn’t want to run the function because “userOrgin” and “userDestination” look ugly and unconventional in normal English?
func getD(from userOrgin: String, to userDestination: String) {
print("From: \(userOrgin), to: \(userDestination)") }getD(from: "🇺🇸", to: "🇰🇷") // From 🇺🇸 to: 🇰🇷
5. Variadic Parameters
Ex) Find the mean from multiple inputs
So you want a function takes input values close to infinity?
Here you go. 👌
var total: Double = 0func findTheMean(numbers: Double...) -> Double {
for number in numbers { total += number }
return total / Double(numbers.count) }findTheMean(numbers: 1, 3, 5, 7, 11) // 5.4
findTheMean(numbers: 1, 4, 8, 16, 25) // 16.2
findTheMean(numbers: 111, 222, 333) // 249