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
More on logging
Last issue included a tip on
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
%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
In the scenario of my crash, the interpolation of “url” and “error” results in surprise template placeholders, because of the presence of spaces in the path that the URL represents. In my tests, a file called “Open Program Ideas File” exists and is used to test some file manipulations. When the path to this file is encoded as a URL, the name of the file becomes “Open%20Program%20Ideas%20File”. Each instance of “%20” followed by a letter is liable to be interpreted by NSLog as a printf-style format specifier. So as far as our filename’s string is concerned, we have “%20P”, “%20I”, and “%20F” threatening to cause trouble.
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.
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.
Software development is a loser’s game | by Ben "The Hosk" Hosking | Medium
I’m not saying developers are losers but most software developers are not beating software development, software development is beating them.
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 🙌