title: [[WWDC 2020]]: Structure Your App For [[SwiftUI]] Previews
---
title: [[WWDC 2020]]: Structure Your App For [[SwiftUI]] Previews
---
- **Metadata**:
-
Date: [[June 26th, 2020]]
-
Conference: [[WWDC 2020]]
-
Speaker(s): [[Kevin Cathey]]
-
Video: https://developer.apple.com/videos/play/wwdc2020/10149/
-
Slides:
-
Tags: [[Talks]]
- Previewing Across Files
- Sample App: Collages
- Pin the preview
- It shows previews for the view you’re in plus pinned views
- Dividers separate them
- Dark mode
- Duplicate preview by clicking +
- Open the Inspector
- Pick Dark Mode
- Pin and Look at it while working in the Asset Catalog
- App Life Cylce
- **Start Debugging** the preview
- Use preview pinning alongside the Gauges
- Use `[[StateObject]]` when you realize things are getting re-initialized every time a view is rendered.
- Sample Data
- Where to define sample data
- Asset Catalog
- [[Development Assets]]
- Can also apply to code
- Structuring Views
- Rich data model
- Simple types
- Views translate rich data type to simple data type
- Examples
- Immutable Inputs
- Identify minimum data that a view needs
- Use simple data types when you can
- Don’t oversimplify due to localization
- Pass `[[PersonNameComponents]]` instead of string
- Mutable Inputs
- Pick out only the pieces that you need to edit
- Use `.constant` binding if you don’t need to change it in previews
- Introduce an intermedia view in a preview to pass in bindings to a preview
- New: [[On Device Previews]]
- Can deploy to multiple versions at once
- Xcode mirrors the preview on device
- Xcode 12 seamlessly updates previews on device
- New [[Xcode Previews]] app
- Generic Inputs
- Use Protocols where the real models are too complex for previews
- Make views generic over the protocol or other view components to simplify at design time
- Sync Status
- Sometimes it’s more convenient to pass data through `[[Environment]]`
- Example: sync status
- Use `.environmentObject` on the preview