Simple example using IfLetStore

Hi everyone.

For some reason I can’t wrap my head around what IfLetStore does and when to use it.

If someone can explain that would be great. Even better if someone can show a simple example.

If there are resources about TCA other then the documentation and point free lessons (already have a subscription). Please let me know

Ps. I’ve looked through the documentation. Still confused and looked through the forum regarding this.

Thanks in advance.

Hi @Muhammed9991! There are a few examples in the demo code. Here's a GitHub search that accumulates some: Search · IfLetStore · GitHub

A pretty self-contained example is here: swift-composable-architecture/01-GettingStarted-OptionalState.swift at 0a38f2c860a64a005afeb8a6fe186eb2818c9a3f · pointfreeco/swift-composable-architecture · GitHub

You can see that the store is scoping onto some optional state and passing it to the IfLetStore view. Then the 2 callback closures are invoked depending on that optional state: then is either invoked with a store of non-optional state, or else is invoked with nothing.

There's another example in the Voice Memos demo, which is used for a "current recording" in state depending on if it's nil or not:

Hope these help!

1 Like

Thank you. That makes a lot of sense. I think I was over complicating it.