SwiftUI error „Unable to infer complex closure return type”

Hello,

i don’t know how to fix this error “Unable to infer complex closure return type; add explicit type to disambiguate”. I tried already everythink (what i can ) and doesn’t work

import SwiftUI

struct ContentView: View {

@State var content: String = “start”

var body: some View {

VStack { // Unable to infer complex closure return type; add explicit type to disambiguate

self.content = try! String(contentsOf: URL(string: “http://zyglarski.pl/swift-http-docs/tinder_list.php”)!)

var contentData = self.content.data(using: .utf8)
var jsDec = JSONDecoder()
var responce = try! jsDec.decode(Candidates.self, from: contentData!)

Text(responce)

}
}
}

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

struct Candidates: Codable {
let candidates:[Candidate]
}
struct Candidate: Codable{
let filename: String
let name: String
let age: String
let distance: String
lei match: String
}

Help, please :slight_smile:

I’ve had the same error and i can never figure out what’s wrong. 

You should not do backend logic in the view. Also you need to return a view in the VStack.
But if you want to do your json stuff, you should move it to the .onAppear

struct ContentView: View {

@State var content: String = “start”
@State var response: Candidates = Candidates()

var body: some View {

VStack {
Text(response)
}
.onAppear {

  let content = try! String(contentsOf: URL(string: “[http://zyglarski.pl/swift-http-docs/tinder_list.php ](http://zyglarski.pl/swift-http-docs/tinder_list.php)”)!)

     let data = content.data(using: .utf8)
     var jsDec = JSONDecoder()
    self.responce = try! jsDec.decode(Candidates.self, from: data!)

}

}
}