1

I've tried this

protocol ErrorableViewProtocol: View {
    var error: Error? { get set }
}

struct ErrorableView: View {
    var normal: any ErrorableViewProtocol
    var error: Error
    
    var body: some View {
        if let error = normal.error {
            ErrorView(error: error)
        } else {
            normal
        }
    }
}

but my knowledge of swift is lacking resulting in:

enter image description here

Please advice what's a good way to show an ErrorView in place or navigate to that once a network error has happened. Thanks.

1 Answer 1

3

You would need to make your View generic:

struct ErrorableView<T: ErrorableViewProtocol>: View {
    var normal: T
    var error: Error
    
    var body: some View {
        if let error = normal.error {
            ErrorView(error: error)
        } else {
            normal
        }
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

How would I make ErrorableView track changes to normal.error and rebuild? currently it does not
@AntonTropashko Take a look at PreferenceKey. But I would think this is out of the scope of this question TBH. Better ask a new one.
Thanks. Looking. The separate question is here stackoverflow.com/questions/74181700/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.