Issue #33
Hi there,
Welcome to the 33rd issue 👋 We're back to a classic five-tips structure, and I hope you find something interesting today.
As an innovation, I've included a new section at the bottom with featured jobs. They're not sponsored this time, but I hope to get sponsored job or indie app ads in the future. The code tips themselves will never have any promoted content in them, only what I truly think is best ☺️
Traversing the view hierarchy
Iterating over superview chain, or parent view controllers, you name it. Awesome trick to replace recursive lookups.
Have you heard of sequence(first:next)
? It's a Sequence
(the same protocol arrays, dictionaries, and other collections conform to), and can be used with for...in
, forEach
, map
, etc.
sequence(first:next) generates an iteratable sequence on the fly, each next element being derived from the previous one. As soon as next
returns nil, iteration stops. So the next
closure can return nil
, but elements in the sequence are non-optional. If nil is never returned, the sequence is infinite.
For example, here's an infinite iteration over even numbers:
for i in sequence(first: 0, next: { $0 + 2 }) { print(i) }
A super neat application for this is to iterate over the view hierarchy, or view controller hierarchy, you name it:
Completion handlers
I really like how we can use typealiases for completion handlers to make code easier to read and refactor:
func do(completion: @escaping ((Result<X, Error>) -> Void)?)
becomes
func do(completion: @escaping CompletionHandler<X>?)
Better TODO warnings
If you're using warnings as TODO reminders, they might be mixing up with real warnings from the compiler. A little productivity tip: adding an emoji to the warnings, and using code snippets makes leaving and viewing TODO-warnings much nicer.
Refactoring a complex View
A short story on refactoring a complex SwiftUI view into smaller views that share the same view model instance. It's a nice approach to making the UI code more maintainable, and even testable.
Efficiently Develop Cleaner SwiftUI Views Using A View Model
Learn how to quickly develop cleaner views by using a view model.
Initialization in Swift
I learned Swift over the course of one summer, and this chapter on Initialization was what stuck with me the most.
Initialization is something we don't think too much about - you write your init method(s) and then if the compiler is not happy, do whatever Xcode suggests to fix it. But in reality, there are carefully thought-out rules and reasons behind restrictions being the way they are.
If you want to learn more about Swift itself, this is a nice read over a cup of something warm 🍁
Initialization — The Swift Programming Language (Swift 5.7)
Initialization is the process of preparing an instance of a class, structure, or enumeration for use.
🧑💻 Featured jobs
Proton (the company behind ProtonMail) has four open iOS positions: iOS (VPN), iOS (Drive), Senior iOS (new product), Engineering Manager (Core iOS). All are available remote and in-office.
🤘
Alright, that’s it for today.
Did you enjoy this issue? Let me know by pressing the buttons below. If you enjoyed it, you can help grow the newsletter by spreading the word ☺️
Got feedback? Want to see more, or less of certain kinds of tips? I’d love to hear from you. Reply to this email or reach out on Twitter via @ios_code_review 🙌
Member discussion