UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Function calls

Forums > SwiftUI

@flmcl  

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]) } } }

2      

hi,

the best i can make of your code is something like this, as the code for some View maybe named MyView:

struct MyView: View {
    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])
//          }
        }
}

there are too many undefined terms in what you've posted, and some of it might be commented out; but your question is why you're getting an error in this section of code:

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

the body property must return a View; you have a function call (not a View) and a VStack, so you'd really want something like this:

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

we'll need more help from you going forward, so please be sure to include enough code to see all of what's going on.

hope that helps,

DMG

2      

What DMG says, You might want to add the .onAppear(perform: findLocations) on the VStack if you do not want to return

2      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.