JoshHolme
JoshHolme

Reputation: 323

Why does swiftUI give me errors for correct code?

SwiftUI has been giving me errors and refusing to build when I know that the code is correct. For instance the following code was working all of 15 minutes ago. I tried to wrap the List in JumpToBookView in a VStack and add a Button underneath the list, and it started spitting out errors on my Image in BookRow. This code was compiling and showing in the preview before I added the Button and VStack, after I removed them, it still won't let me preview the view. I have tried cleaning the build folder, deleting derived data, and quitting the application, all at the same time, and still nothing. No preview, and the project won't compile. For background, the project is mainly UIKit, but I'm trying to incorporate SwiftUI into it, even though that shouldn't matter because this view is not called anywhere. I am currently running the latest version of Xcode, and have attached my code and a screenshot of the errors. For search, I will also paste the errors here. Missing argument label 'imageName:' in call, Value of type 'Image' has no member 'resizable', and Cannot infer contextual base in reference to member 'fit'

enter image description here

import SwiftUI

struct JumpToBookView: View {
    var body: some View {
        List {
            BookRow(selected: true)
            BookRow(selected: true)
            BookRow(selected: true)
            BookRow(selected: true)
            BookRow(selected: true)
            BookRow(selected: true)
        }
    }
    
    func testJump()
    {
        print("test")
    }
}

struct BookRow: View {
    var selected: Bool
    var body: some View {
        ZStack {
            Rectangle()
                .fill(Color(RaiseColor.red))
            HStack{
                Text("1.2 What is an Ecosystem?")
                    .font(.title)
                    .fontWeight(.bold)
                    .foregroundColor(Color(.white))
                Spacer()
                Image(selected ? "pauseButton" : "playButton")
                    .resizable()
                    .aspectRatio(1, contentMode: .fit)
                    .frame(maxWidth: 25)
                    .foregroundColor(Color(.white))
            }
            .padding(.all)
        }
    }
}


struct JumpToBookView_Previews: PreviewProvider {
    static var previews: some View {
        JumpToBookView()
    }
}

EDIT: I just want to add that it was also working with RaiseColor.red before, so there shouldn't be any issues with that custom color.

Upvotes: 1

Views: 2037

Answers (3)

passatgt
passatgt

Reputation: 4442

My issue was that i named an intentdefinition enum to Color. I renamed it and it's working fine again.

Upvotes: 0

JoshHolme
JoshHolme

Reputation: 323

The issue ended up being that my codebase had previously defined a class named Image, so SwiftUI was having issues figuring out which Image I was trying to reference. Without making other changes to the codebase, this issue was resolved by specifying SwiftUI as the parent of Image, which can be done by the following SwiftUI.Image(selected ? "pauseButton" : "playButton")". This fixes all of the issues I was having, but the best thing to do would be to refactor your codebase in order to not have conflicting names with SwiftUI classes and structs.

Upvotes: 1

Asperi
Asperi

Reputation: 258461

When I replace this

Rectangle()
    .fill(Color(RaiseColor.red))

with this

Rectangle()
    .fill(Color.red)

all compiled well... so look for error in that custom color.

Upvotes: 0

Related Questions