devicekit / DeviceKit

DeviceKit is a value-type replacement of UIDevice.
MIT License
4.4k stars 425 forks source link

Failed SwiftUI preview on macOS #341

Closed honghaoz closed 5 months ago

honghaoz commented 1 year ago

Hi!

I'm hitting an issue that the macOS target fails for SwiftUI previews. I understand this package doesn't support macOS, but it does create issues on macOS.

My situation is: I have a Swift package that supports both iOS and macOS platforms.

The package uses

.package(url: "https://github.com/devicekit/DeviceKit", from: "5.0.0"),

in dependencies.

I set the target dependency for iOS platform only:

.product(name: "DeviceKit", package: "DeviceKit", condition: .when(platforms: [.iOS])),

Using the DeviceKit package on macOS target has no issues for normal build.

However, when using SwiftUI preview on macOS target, it always fails. The problem is somehow the SwiftUI preview builds the DeviceKit source code for macOS platform:

CleanShot 2022-11-24 at 21 12 51@2x

I think the fix is straightforward, we should wrap the whole generated code under:

#if os(iOS) || os(watchOS) || os(tvOS)

#endif

This problem happens on the latest Xcode (14.1 (14B47b))

honghaoz commented 1 year ago

Made a PR: https://github.com/devicekit/DeviceKit/pull/342