Group Group Group Group Group Group Group Group Group

Swiftui function Calls

Hello, I can’t seem to call this function anywhere in my code. I get this error: Function declares an opaque return type, but has no return statements in its body from which to infer an underlying type when I place it like this:

var body: some View {
       
        findLocations()
        
        VStack {
            
            MapView(myValue: myValue)
            Spacer()

func findLocations() {
        let myCount = myLocations.count
        // if myCount <= 1 {return mapView}
        for myLocation in myLocations {
            if (myLocation.date! >= WeeklySpan(increment: myValue) && myCount >= 1 ){
                detailArray.append(myLocation.detail!)
                coordArray.append([myLocation.latitude, myLocation.longitude])
            }
        }
    }

Hi @flmcl,
if the code you have above is exactly as you have in your ide,
then you need to add a return before the VStack to make it work

In the code block

   var body: some View {
        VStack { ... }
   }

you need to return something that is a View and that something in your case is VStack however before that you are also calling a function. Return is automatically added if it was just VStack, now that you have added the function call, you need to manually add the return.

   var body: some View {
        callFunction()
        return VStack { ... }
   }

cheers,

1 Like

Depending on what your going to do in that call there are options, ex:

Option 1

struct ContentView: View {

let variable = TempC()
init() {
    variable.GetData()
}
var body: some View {
        Text("Hello World")
}

}
Option 2

struct ContentView: View {

let variable = TempC()

var body: some View {
    Text("Hello World")
    .onAppear {
        self.variable.GetData()
    }
}

}
Similarly you can call it in .onTapGesture or any other, pass reference to your class instance during initialising, etc.

1 Like