Type '()' cannot conform to 'View error

I'm trying to create a function called ResetDay so I can call it on some other view, but I keep getting the error:

Type '()' cannot conform to 'View'; only struct/enum/class types can conform to protocols


class MyClass {
   
    //  ************************* CORE DATA *************************
  
    @FetchRequest(
        entity: Meals.entity(),
        sortDescriptors: []
    )  var mealData: FetchedResults

    @Environment(\.managedObjectContext) var managedObjectContext
   
    //  ****************************************************************
   
    func ResetDay() {
        ForEach(mealData, id: \.self) { meal in
                print(meal.status!)
        }
    }
}


What's going on?

Post not yet marked as solved Up vote post of afern247 Down vote post of afern247
25k views

Replies

In the closure passed to `ForEach`, you need to construct a View.


I belive `print(meal.status!)` does not return a View. Why don't you use usual for-in statement there?

  • In case others see this, ForEach does everything a For loop does except that ForEach is designed to be used with SwiftUI. ForEach returns the needed view.

Add a Comment

I tried:

func ResetDay() {
        for index in 0...self.mealData.count-1 {
           print("\(index)")
        }
    }

and calling it in another view:

Button(action: {
    MyClass().ResetDay()
}) {
       Text("Reset Day")
        .foregroundColor(.red)
}


But it gives me an error: Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)

  • The correct syntax is:

    for i in 0..<mealData.count {

    or:

    for i in Range(0...mealData.count)
Add a Comment

But it gives me an error: Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)

That does not mean you should use ForEach. You need to fix other parts of your code.

Which part? So far this operation only has 3 components, I have't included any logic yet. I have:

1- A brand new swift class with the simple funcion calling 2- coredata and 3- the view from which I'm calling the funcion that has nothing than a simple button


I just tested the same thing within the Struct of the view that I'm using to call the funcion and it works, so the issue is when calling it from another class... interesting...

You have

A brand new swift class with the simple funcion calling which is not managed by SwiftUI

I see what the problem was... I wasn't notifying the view of new data coming from the other class (aka: ObservableObject). But how do I make the data from the loop published?

I tested a simple example:

@Published var counter: Int = 0
    
    func ResetDay() {
        print("\(counter)")
        counter += 1
    }


and that works, but if I try to make coredata published (the wrong way I see...), it gives an error:

@FetchRequest(
        entity: Settings.entity(),
        sortDescriptors: []
    ) @Published var settingsData: FetchedResults


So since the other view isn't aware of coredata settingsData how can I make it aware?

I do not have much experience with `@FetchRequest`, but some SwiftUI annotations does not work as expected under the context where SwiftUI does not manage.


Maybe you should better access your Core Data entity in an old-fashioned way than `@FetchRequest`.

Yeah core data is not too well integrated with swiftui, I'll find a workaround with more old fashion like you said. thanks

Did you end up figuring out what was caused the error?

I got a hint from https://stackoverflow.com/questions/69433485/type-cannot-conform-to-view

 func ResetDay() {
        ForEach(mealData, id: \.self) { meal in
                let _ = print(meal.status!)
        }
    }

You're using the wrong thing. ForEach<Data, ID, Content> is a SwiftUI struct and expects to be provided a view in its content closure.

What you're looking for is Collection.forEach(_ body: (Self.Element) throws -> Void), which iterates over a collection.