Favorite Explanations (of things that made my head hurt)

There are some Swift concepts that really made my head hurt when I was first trying to grasp the concepts. Here are some of my favorite explanations for some of the things I found most difficult to understand. Hope you find them useful as I did!

Associated Types: Using Generics with Swift Protocols

It’s easy to get into a nightmare spaghetti situation of types when you’re first starting with Swift protocols, especially if you try to genericize them. Russ Bishop has a great, easy to follow explanation where he walks you through the “wat?”s of Swift’s associated types.

Should I Use a Struct or Class?

Many people will tell you that you should use struct as much as you can (including me!). But from experience, I have run into cases where I was bending over backward like a dried up Gumby doll to make something a struct, and it ended up being way cleaner when I turned it into a class. Drew Crawford has a great guide for choosing between a struct or class if you ever find yourself in a similar situation. Thanks @hyperspacemark for showing me this!

[weak self] vs [unowned self]

In Swift, like Objective-C, we still have to worry about retain cycles. Even if you are familiar with weak vs strong, unowned might be something new to you. Hector Matos has a fun explanation of when to use [weak self] vs [unowned self] that you should definitely check out! TL; DR: “If you can guarantee that the reference will not be nil at it's point of use, use unowned. If not, then you should be using weak.”

🙌🏼 Let me know what you think of this format! If you like it I can continue having posts like these in addition to my longer form explanation posts. Thanks for reading!