10 Tips to become a better Swift Ninja🏴

This isn’t a part 2. You are now in a different league

Bob Lee
Bob the Developer
4 min readNov 19, 2016

--

My Desk

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.

Today is my 1 month anniversary… How lucky I am to find this phenomenal community…

Like legit. How did it happen so quickly? What’s wrong with this. Last night, I knew I had to get it going.

Twitter last night, trying to deny the truth

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

Most humans have 10 fingers and toes. Don’t you input, pre-initialize.

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)

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]

Type Casting Lesson

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: ??

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.

“Sweet, I love 🍺”— Bob

Switch Statement Lesson

4. Function Custom Parameters

Ex) Print a traveller’s direction

Prints a traveller’s origin and destination

What if the user doesn’t want to run the function because “userOrgin” and “userDestination” look ugly and unconventional in normal English?

5. Variadic Parameters

Ex) Find the mean from multiple inputs

So you want a function takes input values close to infinity?

Here you go. 👌

The content has been migrated from Medium to the personal blog. If you wish to get the full tutorial along, please visit here.

--

--