Wednesday, September 10, 2025
HomeiOS Developmentios - Macro Growth Would not Work in SwiftUI Preview

ios – Macro Growth Would not Work in SwiftUI Preview


I’m making an attempt to make use of customized macro inside a SwiftUI preview. Nevertheless it’s not compiling.

I’ve created an instance Package deal to display the difficulty. You may obtain it right here.

Briefly, I declare a macro to generate a member:

@connected(member, names: named(bar))
macro Foo() = #externalMacro(
    module: "ExamplePackageMacrosImplementation",
    sort: "FooMacro"
)
struct FooMacro: MemberMacro {
    static func enlargement(
        of node: AttributeSyntax,
        providingMembersOf declaration: some DeclGroupSyntax,
        in context: some MacroExpansionContext
    ) throws -> [DeclSyntax] {
        [
            """
            var bar: String { "Lorem ipsum" } 
            """
        ]
    }
}

I connect it to a sort:

@Foo
struct SomeStruct {}

Then, the next Preview does not compile:

#Preview(physique: {
    let object: SomeStruct = .init()
    
    Textual content(object.bar) // Worth of sort 'SomeStruct' has no member 'bar'
})

However this one compiles simply tremendous:

non-public struct ContentView: View {
    let object: SomeStruct = .init()
    
    var physique: some View {
        Textual content(object.bar)
    }
}

#Preview(physique: {
    ContentView()
})

This subject might be attributable to the actual fact, that Preview itself is a macro and must develop. So it doesn’t work hand-in-hand with one other macro enlargement nested inside.

The working method is ok. It is simply that it bloats the codebase a bit of bit. Is there a approach to make this work? In any other case, I would find yourself sending a bug report / characteristic request to Apple.

Thanks.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments