UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: 'Section' cannot be constructed because it has no accessible initializers

Forums > SwiftUI

Feeling like I'm missing something obvious here but the section view doesn't seem to exist.

Image showing error message

If anyone can help me understand what I'm doing wrong it would be greatly appreciated :)

I'm running the latest betas and in an empty project the code works fine.

2      

Hey @Obelix thanks for the feedback.

Here's the code should anyone else have any ideas:

import SwiftUI

struct Test: View {
    var body: some View {
        List {
            Section(header: Text("Important tasks")) {
                Text("Hello, World!")
                Text("Hello, World!")
                Text("Hello, World!")
            }

        }

    }
}

struct Test_Previews: PreviewProvider {
    static var previews: some View {
        Test()
    }
}

I've tried cleaning my build folder, quitting Xcode, deleting derived data, and using the release build of Xode.

My app is mostly swift with UIKit. I wanted to dip my toe into SwiftUI for a very simple menu (just a list).

2      

PS: I once used javascript, google, and a deck of cards to do a magic trick for a users group. This was back in the day when we could pack a high school auditorium with Macintosh users early on a Saturday morning for a few hours of presentations, Q&A, repair workshops, floppy disk sales, and the occasional swag give-away.

MagicKit Pro looks awesome!

2      

From Xcode 13.1 you can create in number of ways

Simple Way (if you only have string text in header)

var body: some View {
    List {
        Section("Important tasks") {
            Text("Hello, World!")
            Text("Hello, World!")
            Text("Hello, World!")
        }
    }
}

If you want a more in header or header and/or footer

var body: some View {
    List {
        Section {
            Text("Hello, World!")
            Text("Hello, World!")
            Text("Hello, World!")
        } header: {
          HStack {
            Text("Important tasks")
            Spacer()
            Text("More…")
          }
        }
    }
}

And this is before Xcode 13 which will be depercated in future

var body: some View {
    List {
        Section(header: Text("Important tasks")) {
            Text("Hello, World!")
            Text("Hello, World!")
            Text("Hello, World!")
        }
    }
}

which work fine with no errors

2      

@Obelix thanks, my first was actually using a dashboard widget. I then moved onto iOS.

@NigelGee I tried with the more modern syntax and still no luck:

It's as if Xcode doesn't know about the Section type. This isn't an issue in a fresh project so it must be something in my spesific project that's causing the issue.

2      

Do you by any chance have a type called Section defined somewhere else in your code that has private or no initializers?

4      

@roosterboy thank you so much this has been driving me mad. I had the same error and it kept complaining, I do in fact have something else defined as a protocol Section just using SwiftUI.Section(...) resolved the issue.

2      

@roosterboy yep yep yep. Had an enum called Section in my project. I had momentarily considered that being the issue but thought Swift had some namespace protection hence dropping all the prefixes for classes.

2      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.