Skip to content

Conversation

Kyle-Ye
Copy link

@Kyle-Ye Kyle-Ye commented Jun 7, 2025

When wrapping a custom assert function for SwiftUI.View, we may want to write the following code.

#if canImport(AppKit)
typealias PlatformHostingController = NSHostingController
#else
typealias PlatformHostingController = UIHostingController
#endif

func viewAssertSnapshot<V: View, Format>(
    of value: @autoclosure () throws -> V,
    as snapshotting: Snapshotting<PlatformHostingController<V>, Format> = .image,
    ...
) {
    assertSnapshot(
        of: PlatformHostingController(rootView: try value()),
        as: snapshotting,
        named: name,
        record: recording,
        timeout: timeout,
        fileID: fileID,
        file: filePath,
        testName: testName,
        line: line,
        column: column
    )
    ...
}

It does not compile now since image extension has a requirement of Value == UIViewController.

And the workaround is change Snapshotting<PlatformHostingController<V>, Format> to Snapshotting<UIViewController, Format>.

I think it is common we define some UIViewController's subclass and use the new subclass as the generic. So I propose we relax the requirement for the common UI extension of Snapshotting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant