Jerome
poitch.bsky.social
Jerome
@poitch.bsky.social
Indie iOS Developer | Amateur Photographer | he/him
6. Finally, I just set an environment object in my preview.

When the environment object is instantiated, the static variable holding the implementation for the API is then set to the mock implementation. At which point any interaction with the View Model will interact with mock data.
July 14, 2025 at 2:42 AM
5. Then, I create a View Model that uses the static variable holding the implementation from step 2.
July 14, 2025 at 2:42 AM
4. I create a class for an environment object.

This is a trick used to instantiate an object for the preview only, which allows me to fill the static variable from the previous step with the mock implementation.
July 14, 2025 at 2:42 AM
3. I create a mock implementation of the API, which stores the data locally.

Side note: I use a struct here to work around the limitation of using an extension.
July 14, 2025 at 2:42 AM
2. I then have a static variable with the actual implementation of the protocol.

This will allow me to initialize that variable differently based on whether I am in a preview or the actual app.

For the real implementation, I would usually set that variable at the App level or similar.
July 14, 2025 at 2:42 AM
1. I define my API as a protocol.

This will enable me to consume the API while maintaining multiple implementations for it.
July 14, 2025 at 2:42 AM
Knowing that .defaultFocus() does not work on iOS, does the accessibility one work?
June 17, 2025 at 4:33 PM