Member-only story

Testing Location Dependent Swift Code

Actually Quite Tricky

Steven Curtis
4 min readDec 15, 2022
Photo by Alvaro Reyes on Unsplash

Difficulty: Beginner | Easy | Normal | Challenging

Have you ever made a rather silly mistake? When coding Swift for your users you probably need a way to allow them to display types of data the way **they** want the data to be displayed.

You’ll also want that data to go beyond formatting. This can be crucial when we think about testing.

So on with the article!

Looking at the code

Look, I’ve created a super-easy project so we can see what is going on in the simulator.

It’s really just 0°C displayed on a plain label in the simulator. You could do the same with SwiftUI, there really aren’t any particular tricks here at all though!

Changing The Location in The Simulator

Location aware? You’ll want you App to know where it is if you’re using the Measurement Api (for example).

Let us play pretend. We want to have an App that only displays the current temperature (it’s only going to display a hardcoded temperature — remember this is a pretend).

I created a viewModel to display the data (this helps unit testing, which I will do later)

--

--

No responses yet