Skip to content

Instantly share code, notes, and snippets.

@auramagi
Last active October 3, 2024 04:07
Show Gist options
  • Save auramagi/07f9dfc61e9228bbbdf9a73ec81a5e00 to your computer and use it in GitHub Desktop.
Save auramagi/07f9dfc61e9228bbbdf9a73ec81a5e00 to your computer and use it in GitHub Desktop.
SwiftUI _viewDebugData
// SwiftUI public _ViewDebug.Property definition
public enum _ViewDebug {
public enum Property : Swift.UInt32, Swift.Hashable {
case type
case value
case transform
case position
case size
case environment
case phase
case layoutComputer
case displayList
public init?(rawValue: Swift.UInt32)
public typealias RawValue = Swift.UInt32
public var rawValue: Swift.UInt32 { get }
}
}
// Output of print(_viewDebugData), formatted for easier viewing
[SwiftUI._ViewDebug.Data(
data: [
SwiftUI._ViewDebug.Property.value: SwiftUI._SafeAreaInsetsModifier(elements: [], nextInsets: nil),
SwiftUI._ViewDebug.Property.displayList: (display-list
(item #:identity 2 #:version 6
(frame (143.66666666666666 354.0; 88.0 20.333333333333332))
(text "Hello World" #:size (88.0, 20.333333333333332))
)
),
SwiftUI._ViewDebug.Property.size: (375.0, 728.0),
SwiftUI._ViewDebug.Property.position: (0.0, 0.0),
SwiftUI._ViewDebug.Property.transform: translate(0.0, -50.0), space(HostingViewCoordinateSpace()),
SwiftUI._ViewDebug.Property.type: SwiftUI._SafeAreaInsetsModifier
],
childData: [
SwiftUI._ViewDebug.Data(
data: [
SwiftUI._ViewDebug.Property.transform: translate(0.0, -50.0), space(HostingViewCoordinateSpace()); sizedSpace(UniqueID(value: 77), 375.0, 728.0),
SwiftUI._ViewDebug.Property.position: (143.5, 353.8333333333333),
SwiftUI._ViewDebug.Property.type: SwiftUI.ModifiedContent<SwiftUI.ModifiedContent<Previews.HelloWorldView, SwiftUI.EditModeScopeModifier>, SwiftUI.HitTestBindingModifier>,
SwiftUI._ViewDebug.Property.value: SwiftUI.ModifiedContent<SwiftUI.ModifiedContent<Previews.HelloWorldView, SwiftUI.EditModeScopeModifier>, SwiftUI.HitTestBindingModifier>(
content: SwiftUI.ModifiedContent<Previews.HelloWorldView, SwiftUI.EditModeScopeModifier>(
content: Previews.HelloWorldView(),
modifier: SwiftUI.EditModeScopeModifier(_editMode: SwiftUI.State<SwiftUI.EditMode>(_value: SwiftUI.EditMode.inactive, _location: nil))
),
modifier: SwiftUI.HitTestBindingModifier()
),
SwiftUI._ViewDebug.Property.size: (88.0, 20.333333333333332)
],
childData: [
SwiftUI._ViewDebug.Data(
data: [
SwiftUI._ViewDebug.Property.value: SwiftUI.HitTestBindingModifier(),
SwiftUI._ViewDebug.Property.type: SwiftUI.HitTestBindingModifier
],
childData: [
SwiftUI._ViewDebug.Data(
data: [
SwiftUI._ViewDebug.Property.type: SwiftUI.ModifiedContent<Previews.HelloWorldView, SwiftUI.EditModeScopeModifier>,
SwiftUI._ViewDebug.Property.value: SwiftUI.ModifiedContent<Previews.HelloWorldView, SwiftUI.EditModeScopeModifier>(
content: Previews.HelloWorldView(),
modifier: SwiftUI.EditModeScopeModifier(_editMode: SwiftUI.State<SwiftUI.EditMode>(_value: SwiftUI.EditMode.inactive, _location: nil))
)
],
childData: [
SwiftUI._ViewDebug.Data(
data: [
SwiftUI._ViewDebug.Property.type: SwiftUI.EditModeScopeModifier,
SwiftUI._ViewDebug.Property.value: SwiftUI.EditModeScopeModifier(_editMode: SwiftUI.State<SwiftUI.EditMode>(_value: SwiftUI.EditMode.inactive, _location: nil))
],
childData: [
SwiftUI._ViewDebug.Data(
data: [
SwiftUI._ViewDebug.Property.value: SwiftUI.ModifiedContent<SwiftUI._ViewModifier_Content<SwiftUI.EditModeScopeModifier>,
SwiftUI._EnvironmentKeyWritingModifier<Swift.Optional<SwiftUI.Binding<SwiftUI.EditMode>>>>(content: SwiftUI._ViewModifier_Content<SwiftUI.EditModeScopeModifier>(), modifier: SwiftUI._EnvironmentKeyWritingModifier<Swift.Optional<SwiftUI.Binding<SwiftUI.EditMode>>>(keyPath: Swift.WritableKeyPath<SwiftUI.EnvironmentValues, Swift.Optional<SwiftUI.Binding<SwiftUI.EditMode>>>, value: Optional(SwiftUI.Binding<SwiftUI.EditMode>(transaction: SwiftUI.Transaction(plist: []), location: SwiftUI.StoredLocation<SwiftUI.EditMode>, _value: SwiftUI.EditMode.inactive)))),
SwiftUI._ViewDebug.Property.type: SwiftUI.ModifiedContent<SwiftUI._ViewModifier_Content<SwiftUI.EditModeScopeModifier>, SwiftUI._EnvironmentKeyWritingModifier<Swift.Optional<SwiftUI.Binding<SwiftUI.EditMode>>>>
],
childData: [
SwiftUI._ViewDebug.Data(
data: [
SwiftUI._ViewDebug.Property.value: SwiftUI._EnvironmentKeyWritingModifier<Swift.Optional<SwiftUI.Binding<SwiftUI.EditMode>>>(keyPath: Swift.WritableKeyPath<SwiftUI.EnvironmentValues, Swift.Optional<SwiftUI.Binding<SwiftUI.EditMode>>>, value: Optional(SwiftUI.Binding<SwiftUI.EditMode>(transaction: SwiftUI.Transaction(plist: []), location: SwiftUI.StoredLocation<SwiftUI.EditMode>, _value: SwiftUI.EditMode.inactive))),
SwiftUI._ViewDebug.Property.type: SwiftUI._EnvironmentKeyWritingModifier<Swift.Optional<SwiftUI.Binding<SwiftUI.EditMode>>>
],
childData: [
SwiftUI._ViewDebug.Data(
data: [
SwiftUI._ViewDebug.Property.value: SwiftUI._ViewModifier_Content<SwiftUI.EditModeScopeModifier>(),
SwiftUI._ViewDebug.Property.type: SwiftUI._ViewModifier_Content<SwiftUI.EditModeScopeModifier>
],
childData: [
SwiftUI._ViewDebug.Data(
data: [
SwiftUI._ViewDebug.Property.value: Previews.HelloWorldView(),
SwiftUI._ViewDebug.Property.type: Previews.HelloWorldView
],
childData: [
SwiftUI._ViewDebug.Data(
data: [
SwiftUI._ViewDebug.Property.value: SwiftUI.Text(storage: SwiftUI.Text.Storage.anyTextStorage(SwiftUI.(unknown context at $10f32f608).LocalizedTextStorage), modifiers: []),
SwiftUI._ViewDebug.Property.type: SwiftUI.Text
],
childData: [
SwiftUI._ViewDebug.Data(
data: [
SwiftUI._ViewDebug.Property.value: SwiftUI.AccessibilityStyledTextContentView(text: Hello World, unresolvedText: SwiftUI.Text(storage: SwiftUI.Text.Storage.anyTextStorage(SwiftUI.(unknown context at $10f32f608).LocalizedTextStorage), modifiers: [])),
SwiftUI._ViewDebug.Property.type: SwiftUI.AccessibilityStyledTextContentView
],
childData: [
SwiftUI._ViewDebug.Data(
data: [
SwiftUI._ViewDebug.Property.type: SwiftUI.ModifiedContent<SwiftUI.StyledTextContentView, SwiftUI.AccessibilityAttachmentModifier>,
SwiftUI._ViewDebug.Property.value: SwiftUI.ModifiedContent<SwiftUI.StyledTextContentView, SwiftUI.AccessibilityAttachmentModifier>(
content: SwiftUI.StyledTextContentView(text: Hello World),
modifier: SwiftUI.AccessibilityAttachmentModifier(storage: SwiftUI.AccessibilityAttachmentModifier.(unknown context at $10f317b48).Storage.propertiesComponent([SwiftUI.(unknown context at $10f317958).ReplacingPropertiesComponent<SwiftUI.AccessibilityNullableOptionSet<SwiftUI.AccessibilityVisibility>>, SwiftUI.(unknown context at $10f317958).ReplacingPropertiesComponent<Swift.Optional<() -> SwiftUI.AXAnyViewTypeDescribingBox>>, SwiftUI.(unknown context at $10f317958).ReplacingPropertiesComponent<Swift.Optional<SwiftUI.Text>>, SwiftUI.(unknown context at $10f317a28).CombiningPropertiesComponent<SwiftUI.AccessibilityNullableOptionSet<SwiftUI.AccessibilityTraitSet>>])))
],
childData: [
SwiftUI._ViewDebug.Data(
data: [
SwiftUI._ViewDebug.Property.type: SwiftUI.AccessibilityAttachmentModifier,
SwiftUI._ViewDebug.Property.value: SwiftUI.AccessibilityAttachmentModifier(storage: SwiftUI.AccessibilityAttachmentModifier.(unknown context at $10f317b48).Storage.propertiesComponent([SwiftUI.(unknown context at $10f317958).ReplacingPropertiesComponent<SwiftUI.AccessibilityNullableOptionSet<SwiftUI.AccessibilityVisibility>>, SwiftUI.(unknown context at $10f317958).ReplacingPropertiesComponent<Swift.Optional<() -> SwiftUI.AXAnyViewTypeDescribingBox>>, SwiftUI.(unknown context at $10f317958).ReplacingPropertiesComponent<Swift.Optional<SwiftUI.Text>>, SwiftUI.(unknown context at $10f317a28).CombiningPropertiesComponent<SwiftUI.AccessibilityNullableOptionSet<SwiftUI.AccessibilityTraitSet>>]))
],
childData: [
SwiftUI._ViewDebug.Data(
data: [
SwiftUI._ViewDebug.Property.type: SwiftUI.StyledTextContentView,
SwiftUI._ViewDebug.Property.displayList: (display-list
(item #:identity 2 #:version 6
(frame (143.66666666666666 354.0; 88.0 20.333333333333332))
(text "Hello World" #:size (88.0, 20.333333333333332))
)
),
SwiftUI._ViewDebug.Property.value: SwiftUI.StyledTextContentView(text: Hello World)
],
childData: []
)])])])])])])])])])])])])])]
@KoCMoHaBTa
Copy link

From where _viewDebugData comes?

@auramagi
Copy link
Author

It's a private property on UIHostingController's view (_UIHostingView):
https://apurin.me/articles/swiftui-secrets/

@KoCMoHaBTa
Copy link

Thanks. Apparently things have changed across SwiftUI versions. This is now called makeViewDebugData and produce Data, which contains a very large JSON.

(lldb) po type(of: view.value(forKey: "makeViewDebugData")!)
Foundation.__NSSwiftData 

I'm not sure how it can be mapped to _ViewDebug, but the unsafeBitCast from the article does not work as is and probably needs some tweaking.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment