View profile

 iOS Code Review | Curated code improvement tips - Issue #11

 iOS Code Review
 iOS Code Review
Hi there,
I hope you enjoy this week’s findings ☀️🙌

On text truncation in SwiftUI
Once I spent a whole day debugging a text truncation issue in SwiftUI. I had a list of views with multiline text in each. All was good, but on rare occasions one of the views would have truncated text. It would be seemingly random - the same item would be truncated or not depending on which position in the list it was in.
Eventually it turned out that I “just” needed to add fixedSize(...) modifier to the Texts. It tells the text to take its ideal size (it’s called intrinsic size in UIKit).
It bugs me that multiline text mostly works without it, so you think all is fine until you catch the truncation on some specific input. Do we have to use it on all multiline texts to avoid these issues? Apparently.
Compiler + enums = 🧡
This tip might be obvious to most devs, but this feature of enums is so useful to me, that it’s worth sharing still. The idea is to always spell out all enum cases in switch statements, so the compiler helps out when a new case is added.
If you use default mainly because there are many cases that don’t need handling, you can list cases with a comma: case .a, .b, .c, .d, .e: break
Toomas Vahter
🎯 Swift tip #3: Avoiding default in switch statements which will trigger a compiler error after adding a new case and forces to review all the related code
#swifttip #swiftlang #xcode #iosdev https://t.co/UpR8ktU7JH
More on logging
Last issue included a tip on print() vs os_log(), and today I have a tip for you if you’re still using NSLog(). The first parameter is a format string, containing 0 or more format specifiers starting with % (fx %@ or %f). We shouldn’t include interpolation in format strings - it’s a matter of time until a printed url or error contains these symbols by accident - causing a crash.
Michael Tsai - Blog - Dangerous NSLog() Calls in Swift
Passing around object ids
Quick tip from this morning on improving readability of your code that works with Identifiable models. This protocol is part of Swift Standard Library, so it’s relevant even if these models are not used in SwiftUI.
Donny Wals 👾
Swift pro-tip for passing around ids for your identifiable objects. Use https://t.co/k5ACbXzuyQ instead of the actual type when possible to make refactoring easier, and to make your intent clearer. https://t.co/LqfZgMzxEC
Donny Wals 👾
Note that this works because the Identifiable protocol has an associated type ID that we can refer to directly. The type of ID is equal to the type of an Identifiable object's id property. So now that would be Int but if we change it, our methods essentially update automatically.
Bugs: not an if, but when
A bit of philosophical content here. It resonated with me a lot - I’ve been training table tennis most of my life, and I can relate to the analogy in this article. Unless you’re significantly better than the opponent, you’re actually just trying to minimize mistakes and keep the ball in play as long as possible.
When we develop long-living software, we’re just trying not to lose - to bugs, technical debt, scaling issues. I love this way of looking at why we do unit tests, code reviews and regular maintenance.
In the creative process of writing code, it’s not if the code will be wrong, it’s when the code will be wrong and fixing it in the easiest way possible.
Software development is a loser’s game | by Ben "The Hosk" Hosking | Medium
🤘
Alright, that’s it for today. 
Did you enjoy this issue? Let me know by pressing the buttons below.
Thanks for your ongoing 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 🙌
Did you enjoy this issue? Yes No
 iOS Code Review
 iOS Code Review @ios_code_review

Bi-weekly newsletter amplifying code improvement tips from the Apple developer community. Swift, Objective-C, iOS, macOS, SwiftUI, UIKit and more. Curated by Marina Gornostaeva and published every other Thursday.

In order to unsubscribe, click here.
If you were forwarded this newsletter and you like it, you can subscribe here.
Created with Revue by Twitter.