1

How can I use a let value in the new #Preview macro in Xcode 15?

Here is my code:

#Preview {
    let categories = Bundle.main.decode([Category].self, from: "Items.json")
    CategoryView(category: categories[0])
}

I have this warning message: Result of 'CategoryView' initializer is unused

If I expand the macro to see what code is generate I can observe an alert message: 'Ambiguous use of 'init(_:traits:body:)' on line 8

@available(iOS 17.0, macOS 14.0, tvOS 17.0, watchOS 10.0, xrOS 1.0, *)
struct $s8Pointers33_4F24814D39CCD226F01562890C575160Ll7PreviewfMf_15PreviewRegistryfMu_: DeveloperToolsSupport.PreviewRegistry {
    static let fileID: String = "Pointers/CategoryView.swift"
    static let line: Int = 33
    static let column: Int = 1

    static func makePreview() throws -> DeveloperToolsSupport.Preview {
        DeveloperToolsSupport.Preview {
            let categories = Bundle.main.decode([Category].self, from: "Items.json")
            CategoryView(category: categories[0])
        }
    }
}

Without #Preview macro my code should be

struct CategoryView_Preview: PreviewProvider {
    static var previews: some View {
        let categories = Bundle.main.decode([Category].self, from: "Items.json")
        CategoryView(category: categories[0])
    }
}

I test it, and it works fine (even in Xcode 15) Any idea to solve this problem on Xcode 15 with the new #Preview macro?

Sébastien REMY
  • 2,399
  • 21
  • 39

0 Answers0