NEW: Learn to build the incredible iOS 15 Weather app today! >>

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.

   

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).

   

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!

   

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

   

@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.

   

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

2      

@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.

   

@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.

   

Hacking with Swift is sponsored by Sentry

SPONSORED With Sentry’s error and performance monitoring for iOS, you see mobile vitals that actually matter, can solve any latency issues quickly, and learn how each release is performing over time.

Learn More

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

Reply to this topic…

You need to create an account or log in to reply.

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.