Skip to content

Unexpected sharing of overrides in SwiftUI Preview traits #322

Answered by stephencelis
cabeca asked this question in Q&A
Discussion options

You must be logged in to vote

@cabeca This is a quirk/bug of Xcode previews. If you have multiple defined in a single file, they will all execute even though you're only viewing a single one. You should be able to reproduce this in a vanilla project, so we think it's worth filing feedback if you find the behavior surprising.

Preview traits only provide a "modifier" extension to apply to a view after it's been computed, which we can't leverage, so instead our trait is simply a shorthand for prepareDependencies, which globally overrides dependencies for the process. This global is going to bleed over to other previews defined in the same file as a result. We're open to ideas on how to improve this, but we're not sure it…

Replies: 1 comment 5 replies

Comment options

You must be logged in to vote
5 replies
@cabeca
Comment options

@mbrandonw
Comment options

@cabeca
Comment options

@mbrandonw
Comment options

@cabeca
Comment options

Answer selected by cabeca
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants