Swift quiz App Xcode throwing error

Hello Community,

I was doing SwiftUI Views and basically tipped it off. But my code throws an error however the code from codecademy which is basically the same works fine. It’s for a quiz app.

//
// ContentView.swift
// Quiz App Codecademy Project I
//
// Created by ------ on 27.05.23.
//

import SwiftUI

struct ContentView: View {
var body: some View {
ZStack
{
mainColor.ignoresSafeArea()
VStack
{
Text(“1 / 10”)
.font(.callout)
.multilineTextAlignment(.leading)
.padding()
Text(“What was the first computer bug?”)
.font(.largeTitle)
.bold()
.multilineTextAlignment(.leading)
Spacer()
HStack {
Button(action: {
print(“Tapped on Choice 1”)
}, label: {
Text(“Ant”)
.font(.body)
.bold()
.multilineTextAlignment(.center)
.padding() Here comes the error: Type ‘(Color?) → some View’ cannot
conform to ‘ShapeStyle’
.border(accentColor, width: 4)
})
Button(action: {
print(“Tapped on Choice 2”)
}, label: {
Text(“Beetle”)
.font(.body)
.bold()
.multilineTextAlignment(.center)
.padding()
.border(accentColor, width: 4)
})
Button(action: {
print(“Tapped on Choice 3”)
}, label: {
Text(“Moth”)
.font(.body)
.bold()
.multilineTextAlignment(.center)
.padding()
.border(accentColor, width: 4)
})
Button(action: {
print(“Tapped on Choice 4”)
}, label: {
Text(“Fly”)
.font(.body)
.bold()
.multilineTextAlignment(.center)
.padding()
.border(accentColor, width: 4)

                })
            }
        }
        
    }
    .foregroundColor(.white)
}

}

struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}

let mainColor = Color(red: 20/255, green: 28/255, blue: 58/255)
let accentColor = Color(red: 48/255, green:105/255, blue: 240/255)

I’m having this also. The problem appears to be with using accentColor within the .border method.
I could only get the error to stop by doing this instead: .border(Color.blue, width: 4)

2 Likes

Dude thank you it showed the error on padding() and I was just messing around with it. You probably saved me a few hours for when I continue the project. Thank you!

2 Likes

I have just discovered an even better solution as this was bothering me. You can in fact use accentColor as originally described within the lesson however, accentColor must be defined within the body of Struct ContentView: View { ...}. Not in the global scope of the editor. The Codecademy lesson doesn’t make this particularly obvious and this should probably be clarified.

Sorry but do you mean by writing
accentColor = Color.blue
Or how do I define accentColor?
It should definitely be clarified in the course. Thank you for your help.

So you define accentColor as you already have. But you define it within the body of the Struct ContentView: View {...} code block. So you can just cut and paste it in to move it.

Sorry, but I can’t follow can you maybe share a screenshot?

Haha it’s fine. Look at your code above. You see the line Struct ContentView: View {…?
Put the var accentColor = ... after that curly bracket so that it is contained within the body of that.

1 Like

I forgot the var. Damit :joy:
Thank you very much for your patience.
Have a nice day

1 Like

var or let it does not matter. As long as you place that piece of code within the body of the code that I mention above.

1 Like