Inconsistent Shared override in #Preview between iOS and macOS #189
-
DescriptionI want to override shared data for the preview. The following code works fine on iOS, but has no effect on macOS. I am on the latest released versions. The attached screen shots have both the code and the output in the preview. The iOS version correctly shows ![]() ![]() Checklist
Expected behaviorI expect that I can override the shared data. Actual behaviorI can override the shared data for Reproducing projectiOS: SharingTest.zip macOS: SharingTestMac.zip Sharing version information2.7.2 Destination operating systemiOS 18.6 Xcode version information16.4 Swift Compiler version informationswift-driver version: 1.120.5 Apple Swift version 6.1.2 (swiftlang-6.1.2.1.2 clang-1700.0.13.5)
Target: arm64-apple-macosx15.0 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
As far as I can tell this is one (of many) quirks of the struct ContentViewPreview: PreviewProvider {
static var previews: some View {
@Shared(.runtimeData) var runtimeData = .mock
ContentView()
.frame(width: 300, height: 600)
}
} Alternately, you could force a mutation of the #Preview {
- @Previewable @Shared(.runtimeData) var runtimeData = .mock
+ @Previewable @Shared(.runtimeData) var runtimeData
+ let _ = $runtimeData.withLock { $0 = .mock }
ContentView()
.frame(width: 300, height: 600)
} Because this seems to be a bug/behavior with |
Beta Was this translation helpful? Give feedback.
As far as I can tell this is one (of many) quirks of the
#Preview
macro. If you use the old style of preview provider it works as expected:Alternately, you could force a mutation of the
@Shared
value:Because this seems to be a bug/behavior with
#Preview
and not our libr…