Strange SwiftUI bug (just making sure if it's a bug or not)

My code:

struct PopoverMessage: View {
    var body: some View {
        VStack(alignment: .leading) {
            Text("You need to launch this game at Teminal.")
                .font(.title)
                .fontWeight(.light)
            Divider()
            HStack {
                Text("Help me generate launch command ")
                    .fontWeight(.thin)
                Image(systemName: "rectangle.portrait.and.arrow.right.fill")
                    .resizable()
                    .foregroundColor(.gray)
                    .frame(width: 10, height: 10)
            }.padding(10)
        }
    }
}

works while


struct PopoverMessage: View {
    var body: some View {
        VStack(alignment: .leading) {
            Text("You need to launch this game at Teminal.")
                .font(.title)
                .fontWeight(.light)
            Divider()
            HStack {
                Text("Help me generate launch command ")
                    .fontWeight(.thin)
                Image(systemName: "rectangle.portrait.and.arrow.right.fill")
.weigh
                    .resizable()
                    .foregroundColor(.gray)
                    .frame(width: 10, height: 10)
            }.padding(10)
        }
    }
}

don't and displays a error message Trailing closure passed to parameter of type 'CGFloat?' that does not accept a closure Here's the text in the diagnostics sheet:

trailing closure passed to parameter of type 'CGFloat?' that does not accept a closure



----------------------------------------



CompileDylibError: Failed to build ContentView.swift



Compiling failed: trailing closure passed to parameter of type 'CGFloat?' that does not accept a closure



/Users/wangkeshijian/Documents/.myFolder/.system/TerminalAdventure/TerminalAdventure/ContentView.swift:12:37: error: trailing closure passed to parameter of type 'CGFloat?' that does not accept a closure

        VStack(alignment: .leading) {

                                    ^

SwiftUI.VStack:4:12: note: 'init(alignment:spacing:)' declared here

    public init(alignment: HorizontalAlignment = .center, spacing: CGFloat? = nil)

           ^

// my comment here: it seems that SwiftUI, under some sort of strange bug, did notrecognize my initializer 



==================================



|  BuildInvocationError

|  

|  /Users/wangkeshijian/Documents/.myFolder/.system/.Downloads/Xcode14-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc -F /Users/wangkeshijian/Documents/.myFolder/.system/.Downloads/Xcode14-beta.app/Contents/SharedFrameworks -enforce-exclusivity=checked -DDEBUG -sdk /Users/wangkeshijian/Documents/.myFolder/.system/.Downloads/Xcode14-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX13.0.sdk -target x86_64-apple-macos12.4 -module-cache-path /Users/wangkeshijian/Library/Developer/Xcode/DerivedData/ModuleCache.noindex -Xfrontend -serialize-debugging-options -enable-testing -swift-version 5 -enable-bare-slash-regex -I /Users/wangkeshijian/Library/Developer/Xcode/DerivedData/TerminalAdventure-fdqrafmgjltnvaesvnkdwzpsebjf/Build/Intermediates.noindex/Previews/TerminalAdventure/Products/Debug -F /Users/wangkeshijian/Library/Developer/Xcode/DerivedData/TerminalAdventure-fdqrafmgjltnvaesvnkdwzpsebjf/Build/Intermediates.noindex/Previews/TerminalAdventure/Products/Debug -emit-localized-strings -emit-localized-strings-path /Users/wangkeshijian/Library/Developer/Xcode/DerivedData/TerminalAdventure-fdqrafmgjltnvaesvnkdwzpsebjf/Build/Intermediates.noindex/Previews/TerminalAdventure/Intermediates.noindex/TerminalAdventure.build/Debug/TerminalAdventure.build/Objects-normal/x86_64 -c -j4 -serialize-diagnostics -Xcc -I/Users/wangkeshijian/Library/Developer/Xcode/DerivedData/TerminalAdventure-fdqrafmgjltnvaesvnkdwzpsebjf/Build/Intermediates.noindex/Previews/TerminalAdventure/Intermediates.noindex/TerminalAdventure.build/Debug/TerminalAdventure.build/swift-overrides.hmap -Xcc -iquote -Xcc /Users/wangkeshijian/Library/Developer/Xcode/DerivedData/TerminalAdventure-fdqrafmgjltnvaesvnkdwzpsebjf/Build/Intermediates.noindex/Previews/TerminalAdventure/Intermediates.noindex/TerminalAdventure.build/Debug/TerminalAdventure.build/TerminalAdventure-generated-files.hmap -Xcc -I/Users/wangkeshijian/Library/Developer/Xcode/DerivedData/TerminalAdventure-fdqrafmgjltnvaesvnkdwzpsebjf/Build/Intermediates.noindex/Previews/TerminalAdventure/Intermediates.noindex/TerminalAdventure.build/Debug/TerminalAdventure.build/TerminalAdventure-own-target-headers.hmap -Xcc -I/Users/wangkeshijian/Library/Developer/Xcode/DerivedData/TerminalAdventure-fdqrafmgjltnvaesvnkdwzpsebjf/Build/Intermediates.noindex/Previews/TerminalAdventure/Intermediates.noindex/TerminalAdventure.build/Debug/TerminalAdventure.build/TerminalAdventure-all-target-headers.hmap -Xcc -iquote -Xcc /Users/wangkeshijian/Library/Developer/Xcode/DerivedData/TerminalAdventure-fdqrafmgjltnvaesvnkdwzpsebjf/Build/Intermediates.noindex/Previews/TerminalAdventure/Intermediates.noindex/TerminalAdventure.build/Debug/TerminalAdventure.build/TerminalAdventure-project-headers.hmap -Xcc -I/Users/wangkeshijian/Library/Developer/Xcode/DerivedData/TerminalAdventure-fdqrafmgjltnvaesvnkdwzpsebjf/Build/Intermediates.noindex/Previews/TerminalAdventure/Products/Debug/include -Xcc -I/Users/wangkeshijian/Library/Developer/Xcode/DerivedData/TerminalAdventure-fdqrafmgjltnvaesvnkdwzpsebjf/Build/Intermediates.noindex/Previews/TerminalAdventure/Intermediates.noindex/TerminalAdventure.build/Debug/TerminalAdventure.build/DerivedSources-normal/x86_64 -Xcc -I/Users/wangkeshijian/Library/Developer/Xcode/DerivedData/TerminalAdventure-fdqrafmgjltnvaesvnkdwzpsebjf/Build/Intermediates.noindex/Previews/TerminalAdventure/Intermediates.noindex/TerminalAdventure.build/Debug/TerminalAdventure.build/DerivedSources/x86_64 -Xcc -I/Users/wangkeshijian/Library/Developer/Xcode/DerivedData/TerminalAdventure-fdqrafmgjltnvaesvnkdwzpsebjf/Build/Intermediates.noindex/Previews/TerminalAdventure/Intermediates.noindex/TerminalAdventure.build/Debug/TerminalAdventure.build/DerivedSources -Xcc -DDEBUG=1 -working-directory /Users/wangkeshijian/Documents/.myFolder/.system/TerminalAdventure /Users/wangkeshijian/Library/Developer/Xcode/DerivedData/TerminalAdventure-fdqrafmgjltnvaesvnkdwzpsebjf/Build/Intermediates.noindex/Previews/TerminalAdventure/Intermediates.noindex/TerminalAdventure.build/Debug/TerminalAdventure.build/Objects-normal/x86_64/ContentView.1.preview-thunk.swift -o /Users/wangkeshijian/Library/Developer/Xcode/DerivedData/TerminalAdventure-fdqrafmgjltnvaesvnkdwzpsebjf/Build/Intermediates.noindex/Previews/TerminalAdventure/Intermediates.noindex/TerminalAdventure.build/Debug/TerminalAdventure.build/Objects-normal/x86_64/ContentView.1.preview-thunk.o -module-name TerminalAdventure_PreviewReplacement_ContentView_1 -Onone -Xfrontend -disable-modules-validate-system-headers -gline-tables-only
Answered by absar in 717671022

Hi, I am also struggling to discover whether its a bug or some user-driven mistake..:-) But, I have found a solution to it which works most of the time.

I just pass the alignment and spacing parameters (together) and it fixes the problem whether this error pops-up in VStack or HStack Example : alignment: .center, spacing: 0 It fails when I don't apply both parameters together.

If you manage to find the reason for this strange behaviour please update this post accordingly. Many thanks..

I think there is a typo .weigh after Image(systemName: "rectangle.portrait.and.arrow.right.fill")

Accepted Answer

Hi, I am also struggling to discover whether its a bug or some user-driven mistake..:-) But, I have found a solution to it which works most of the time.

I just pass the alignment and spacing parameters (together) and it fixes the problem whether this error pops-up in VStack or HStack Example : alignment: .center, spacing: 0 It fails when I don't apply both parameters together.

If you manage to find the reason for this strange behaviour please update this post accordingly. Many thanks..

@newwbee oh I add it afterwards in devforums so there might be a typo😂

OK I found out why

Compiler found a bug in our code of the body of the Stack

Instead of telling SwiftUI checking the body of the VStack it thinks it's a faulty initialization

So when we use @absar's method it forced SwiftUI into initalizing as it should and therefore, find out the bug in our code in body.

or rather:

VStack(body: {
....
})

but after you fix your bug it should be back to normal.

Please continue to post if you have any other infos. Thanks!

Strange SwiftUI bug (just making sure if it's a bug or not)
 
 
Q