3 min read

Issue #58

Hi there, welcome to the 58th issue of iOS Code Review. I hope you find something interesting for you today ☺️

💎 Mobile DevOps Health Check - the first industry benchmarking tool

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 ☺️

How (not) to monitor SwiftUI @State
While I was working on the first version of my latest app SharePal ⚡️, I figured that I’d like to add haptic feedback for distinct action within the app. Something that did not worked well… Since I’m targeting iOS 16 as a base, I…

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)
  }
}
Generating Random Numbers Elegantly in Swift
Swift has a useful mechanism to generate random values. Today, let’s see how we can plug in SystemRandomNumberGenerator for our own types.

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."

Launching a SwiftUI view from the terminal
Learn how to launch a SwiftUI view directly from your Swift command-line tool without making a full-blown macOS application.

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:

Writing good unit tests
We write tests to prove that our code is correct now and in the future. If we want to do some refactoring some day then we can rely on…

✌️
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 ❤️