when compiling code in swiftui, returns with error message

import SwiftUI

struct PokemonDetailView: View {   @EnvironmentObject var vm: ViewModel   let pokemon: Pokemon       var body: some View {     VStack {       PokemonView(pokemon: pokemon)               VStack(spacing: 10) {         Text("ID: (vm.pokemonDetails?.id ?? 0)")         Text("Weight: (vm.formatHW(value: vm.pokemonDetails?.weight ?? 0)) KG")         Text("Height: ((vm.formatHW(value: vm.pokemonDetails?.height ?? 0)) M")       }       .padding()     }     .onAppear {       vm.getDetails(pokemon: pokemon) }   }     }

struct PokemonDetailView_Previews: PreviewProvider {   static var previews: some View {     PokemonDetailView(pokemon: Pokemon.samplePokemon)       .environmentObject(ViewModel())   } }

"error message" :

in line:     Text("ID: (vm.pokemonDetails?.id ?? 0)")         Text("Weight: (vm.formatHW(value: vm.pokemonDetails?.weight ?? 0)) KG")         Text("Height: ((vm.formatHW(value: vm.pokemonDetails?.height ?? 0)) M")

"Cannot call value of non-function type 'Binding'

Referencing subscript 'subscript(dynamicMember:)' requires wrapper 'EnvironmentObject.Wrapper'

insert '$'

Value of type 'ViewModel' has no dynamic member 'formatHW' using key path from root type 'ViewModel'"

In the code you posted the \ is missing from the string interpolation. Text("\(yourVar)") would be correct not Text("(yourVar)")

@VictorZarzar

  • \ is missing, but that's not the cause of the error. As written, the following string will be displayed: ID: (vm.pokemonDetails?.id ?? 0)
  • also an extra open parenthesis after "Height:"

But you should show more code, notably how ViewModel is defined and where you have defined Binding.

And also show exactly the error messages, and tell on which line.

Note: when you post code, please use code formatter tool (</>| button below).

import SwiftUI
struct PokemonDetailView: View {
    @EnvironmentObject var vm: ViewModel
    let pokemon: Pokemon
    
    var body: some View {
        VStack {
            PokemonView(pokemon: pokemon)
            VStack(spacing: 10) {
                Text("ID: (vm.pokemonDetails?.id ?? 0)")
                Text("Weight: (vm.formatHW(value: vm.pokemonDetails?.weight ?? 0)) KG")
                Text("Height: ((vm.formatHW(value: vm.pokemonDetails?.height ?? 0)) M")
            }
            .padding()
        }
        .onAppear {
            vm.getDetails(pokemon: pokemon)
        }
    }
}

struct PokemonDetailView_Previews: PreviewProvider {
    static var previews: some View {
        PokemonDetailView(pokemon: Pokemon.samplePokemon)
            .environmentObject(ViewModel())
    }
}
when compiling code in swiftui, returns with error message
 
 
Q