Issue #58
Hi there, welcome to the 58th issue of iOS Code Review. I hope you find something interesting for you today ☺️
How well does your Mobile DevOps team perform? Take the quick Mobile DevOps Health Check to find out how mature your team is and how you compare against the industry’s top-performing apps.
Take the 6-question Health Check
Sponsors help me grow my content creation and keep the lights up, so if you have time please make sure to have a look at the survey they're offering ☺️
How (not) to monitor SwiftUI @State
It might be tempting to use didSet
to observe variables declared with @State
, especially that it seems to work in the basic cases. But it doesn't work when the binding is passed to another view. We need to use the .onChange
modifier to observe the variable. I met Dean at NSSpain this year, and I'm happy to share his blog with you all ☺️
Random enum case
Such elegant solution for accessing a random case of an enum. Here's a gist of it, by a adding a simple extension on CaseIterable
, any enum gets such functionality. You can read more about it in Jordan's blog post below.
enum Foo: String, CaseIterable {
case a, b, c
}
print(Foo.randomCaseIterableElement())
extension CaseIterable {
public static func randomCaseIterableElement(using generator: inout some RandomNumberGenerator) -> Self? {
allCases.randomElement(using: &generator)
}
public static func randomCaseIterableElement() -> Self? {
var generator = SystemRandomNumberGenerator()
return randomCaseIterableElement(using: &generator)
}
}
Launching a SwiftUI view from the terminal
"While there is a lot you can display in the terminal, there is certain information that is better conveyed in a graphical user interface. Contrary to what you might think, you don't need to create a full-blown macOS app to display a simple UI."
Writing better unit tests
A collection of before-and-after examples that demonstrate how to enhance test cases by verifying prerequisites and conditions.
verify(StorageThing)
.hasNoEntry("something")
sut = UnitBeingTested()
verify(StorageThing)
.hasEntry("something")
In this example, the first line brings a benefit of verifying that the mocked storage didn't have the value before. Otherwise, the test would succeed even if initialising sut
didn't save the value.
You can find many more examples like that in the article:
✌️
Alright, that's it for today! Let's spread the good code vibes ✨🧘🌈☀️
I'm curious if you found any of the tips particularly interesting - let me know by replying to this email!
Thank you to Bitrise for sponsoring yet another issue ❤️
Member discussion