Skip to content

Commit

Permalink
Fix if crash (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
noahsmartin authored Oct 2, 2023
1 parent d19f8cd commit 8258f1f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
10 changes: 10 additions & 0 deletions DemoApp/DemoApp/TestViews/EmptyView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,18 @@ struct EmptyViewTest: View {
}
}

struct EmptyIfTest: View {
var body: some View {
if true {
EmptyView()
}
Text("Hello World")
}
}

struct EmptyViewTest_Previews: PreviewProvider {
static var previews: some View {
EmptyViewTest()
EmptyIfTest()
}
}
6 changes: 5 additions & 1 deletion Sources/SnapshotPreviewsCore/ViewInspection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ public enum ViewInspection {
private static func children(of view: some View) -> [(any View, [any ViewModifier])] {
let viewType = type(of: view)
let typeName = String(reflecting: viewType)
if typeName.starts(with: "SwiftUI._ConditionalContent") {
if typeName.starts(with: "Swift.Optional<") {
if let child = Mirror(reflecting: view).children.first?.value as? any View {
return children(of: child)
}
} else if typeName.starts(with: "SwiftUI._ConditionalContent") {
let storage = Self.attribute(label: "storage", value: view)!
if let trueContent = Self.attribute(label: "trueContent", value: storage) as? any View {
return childrenIfMultiple(of: trueContent)
Expand Down

0 comments on commit 8258f1f

Please sign in to comment.