Using @Namespace in #Preview Xcode 15 gives an error message

Greetings...

I am trying to use @Namespace for my matchedGeometryEffect use case.

prior to Xcode 15 beta the following code worked just fine:

struct ChapterItem_Previews: PreviewProvider {
   @Namespace static var namespace
   
    static var previews: some View {
       ChapterItem(namespace: namespace, show: .constant(true))
    }
}

However trying to do the same within the new Xcode 15 beta #Preview Macro

#Preview {
    @Namespace var namespace
    ChapterItem(namespace: namespace, show: .constant(true))
}

produces the following error message:

Ambiguous use of 'Preview(_:traits:body:)'

May I kindly get assistance on the proper way I can get this to work in Xcode 15 beta? Please be as detail as you can since I'm still new to swiftUI as well Thank You.

Accepted Reply

The closure passed to #Preview is not a ViewBuilder so once you have more than a single line code in it the "implicit return" no longer functions and you therefore have to be explicit about which lines return. I believe the error above will go away if you change line 64 to read return ChapterItem(namespace: namespace, show: .constant(true)).

  • This is counter-intuitive. Also, it seems to have changed between versions. I am working through an older Udemy course, and at that time (2020) it seemed to work without any issues.

Add a Comment

Replies

The closure passed to #Preview is not a ViewBuilder so once you have more than a single line code in it the "implicit return" no longer functions and you therefore have to be explicit about which lines return. I believe the error above will go away if you change line 64 to read return ChapterItem(namespace: namespace, show: .constant(true)).

  • This is counter-intuitive. Also, it seems to have changed between versions. I am working through an older Udemy course, and at that time (2020) it seemed to work without any issues.

Add a Comment

I have the same problem

  • could you post the code and the error you are seeing in Xcode?

Add a Comment