{"dataset":{"id":"00000000-0000-0000-0000-000000000056","slug":"swiftui-best-practices","name":"SwiftUI best practices","description":"Source-linked guidance on SwiftUI fundamentals: views and view composition, state and data flow, layouts and stacks, navigation, and previews and testing. Scoped to current SwiftUI docs.","visibility":"public","version":1,"documentCount":5,"publisher":"Koskamo editorial","license":"Original summaries; linked sources retain their own terms.","reviewedAt":"2026-10-04T00:00:00.000Z","readOnly":true,"createdAt":"2026-10-04T00:00:00.000Z","updatedAt":"2026-10-04T00:00:00.000Z"},"total":5,"documents":[{"title":"XCTest","sourceUrl":"https://developer.apple.com/tutorials/data/documentation/xctest.json","text":"Use the XCTest framework to write unit tests that integrate seamlessly with Xcode’s testing workflow. Tests assert that certain conditions are satisfied during code execution and record failures with optional messages; tests can also measure the performance of blocks of code to check for performance regressions. Use XCTest with XCUIAutomation to interact with an application’s UI and validate user interaction flows. Xcode 16 and later includes Swift Testing, a framework for writing unit tests that takes advantage of Swift’s capabilities; consider it for new unit test development and migrating existing tests. A test target can contain tests using both frameworks, but do not mix API from the two in the same test; continue to use XCTest for user interface tests and performance tests. Scope: SwiftUI. Reviewed October 4, 2026.","id":"swiftui-best-practices:XCTest","version":1,"createdAt":"2026-10-04T00:00:00.000Z","updatedAt":"2026-10-04T00:00:00.000Z"},{"title":"NavigationStack","sourceUrl":"https://developer.apple.com/tutorials/data/documentation/swiftui/navigationstack.json","text":"Use a navigation stack to present a stack of views over a root view. People can add views to the top of the stack by clicking or tapping a NavigationLink, and remove views using built-in, platform-appropriate controls like a Back button or a swipe gesture. The stack always displays the most recently added view that hasn’t been removed and doesn’t allow the root view to be removed. To create navigation links, associate a view with a data type by adding a navigationDestination(for:destination:) modifier inside the stack’s view hierarchy, then initialize a NavigationLink that presents an instance of the same kind of data. Navigating back removes the detail view and reveals the root view again. Scope: SwiftUI. Reviewed October 4, 2026.","id":"swiftui-best-practices:NavigationStack","version":1,"createdAt":"2026-10-04T00:00:00.000Z","updatedAt":"2026-10-04T00:00:00.000Z"},{"title":"HStack","sourceUrl":"https://developer.apple.com/tutorials/data/documentation/swiftui/hstack.json","text":"Unlike LazyHStack, which renders views only when displayed onscreen, HStack renders all subviews simultaneously regardless of on- or offscreen status. Use regular HStack for small subview counts or when avoiding the lazy version’s delayed rendering behavior. For simple horizontal arrangements, such as five text views, specify alignment and spacing parameters within the stack initializer. If a horizontal stack must conform to the Layout protocol, particularly when implementing conditional layouts via AnyLayout, utilize HStackLayout instead. Scope: SwiftUI. Reviewed October 4, 2026.","id":"swiftui-best-practices:HStack","version":1,"createdAt":"2026-10-04T00:00:00.000Z","updatedAt":"2026-10-04T00:00:00.000Z"},{"title":"Managing model data in your app","sourceUrl":"https://developer.apple.com/tutorials/data/documentation/swiftui/managing-model-data-in-your-app.json","text":"When environment object availability is unguaranteed, retrieve an optional version to safely return nil instead of throwing an exception. To reflect changed data, use Observation in SwiftUI, allowing views to update without property wrappers or bindings, such as toggling a book’s property in a button’s action closure. However, when a view expects a binding to modify mutable properties, wrap model data with the @Bindable property wrapper. This enables using components like TextField or Toggle to change properties, utilizing the $ syntax to pass a binding to each property. Scope: SwiftUI. Reviewed October 4, 2026.","id":"swiftui-best-practices:Managing model data in your app","version":1,"createdAt":"2026-10-04T00:00:00.000Z","updatedAt":"2026-10-04T00:00:00.000Z"},{"title":"Creating and combining views","sourceUrl":"https://developer.apple.com/tutorials/data/tutorials/swiftui/creating-and-combining-views.json","text":"Modifiers wrap a view to change its display or other properties; each modifier returns a new view, so it is common to chain multiple modifiers, stacked vertically. Use the inspector to change or remove a modifier — Xcode updates your code immediately, and your code is always the source of truth. Changing the Font modifier to “Title” applies the system font so text responds to the user’s preferred font sizes and settings; `foregroundColor(.green)` changes the text’s color. Stacks group views together horizontally, vertically, or back-to-front, combining multiple views because the `body` property only returns a single view. Control-click a view’s initializer to embed it, for example choosing “Embed in VStack”, or drag a `Text` view from the library and set its font to `subheadline`. Scope: SwiftUI. Reviewed October 4, 2026.","id":"swiftui-best-practices:Creating and combining views","version":1,"createdAt":"2026-10-04T00:00:00.000Z","updatedAt":"2026-10-04T00:00:00.000Z"}]}