Value of type ' Module<Wallet_App>' has no subscript please check the below code

import SwiftUI

struct List_View: View {

    var Wallet : Wallet_Data

    var body: some View {

        

            ZStack {

                ListRectShape()

                    .fill(Color.background)

                .frame(width: 368, height: 75)

                Wallet.image

                    .padding(.trailing, 280.0)

                Text("Amazon")

                    .font(.title3)

                    .fontWeight(.light)

                    .foregroundColor(Color.white)

                    .multilineTextAlignment(.leading)

                    .padding(.top, -25.0)

                    .padding(.trailing, 125.0)

                Text("Augest 10 2022")

                    .font(.body)

                    .fontWeight(.light)

                    .foregroundColor(Color.white)

                    .padding(.trailing, 78.0)

                    .padding(.top, 21.0)

                ListRectShape()

                    .fill(Color.background)

                    .frame(width: 83, height: 33)

                    .border(Color.white)

                    .padding(.leading, 225.0)

                Text("$109.08")

                    .foregroundColor(Color.white)

                    .padding(.leading, 225.0)

                

                    

            

            }

        

        ZStack {

            ListRectShape()

                .fill(Color.background)

            .frame(width: 368, height: 75)

            Image("Apple WC")

                .padding(.trailing, 280.0)

            Text("Apple")

                .font(.title3)

                .fontWeight(.light)

                .foregroundColor(Color.white)

                .multilineTextAlignment(.leading)

                .padding(.top, -22.0)

                .padding(.trailing, 145.0)

            Text("Augest 10 2022")

                .font(.body)

                .fontWeight(.light)

                .foregroundColor(Color.white)

                .padding(.trailing, 78.0)

                .padding(.top, 21.0)

            ListRectShape()

                .fill(Color.background)

                .frame(width: 83, height: 33)

                .border(Color.white)

                .padding(.leading, 225.0)

            Text("$1000.00")

                .foregroundColor(Color.white)

                .padding(.leading, 225.0)

            

        

        }

            

    }

}

struct List_View_Previews: PreviewProvider {

    static var previews: some View {

        List_View(Wallet: Wallet_App[0]) // here is were I'm getting an error

    }

}

//this is my ModelData were I'm trying to load my json file

import Foundation

import Combine

final class ModelData : ObservableObject{

  var Wallet_App: [Wallet_Data] = load("Contents.json")

}

func load<T: Decodable>(_ filename: String) -> T {

    let data: Data

    guard let file = Bundle.main.url(forResource: filename, withExtension: nil)

        else {

            fatalError("Couldn't find (filename) in main bundle.")

    }

    do {

        data = try Data(contentsOf: file)

    } catch {

        fatalError("Couldn't load (filename) from main bundle:\n(error)")

    }

    do {

        let decoder = JSONDecoder()

        return try decoder.decode(T.self, from: data)

    } catch {

        fatalError("Couldn't parse (filename) as (T.self):\n(error)")

    }

}

Answered by Claude31 in 724497022

Please use code formatter when you post code. That makes it much easier to read.

import SwiftUI
struct List_View: View {
    var Wallet : Wallet_Data
    var body: some View {
        
            ZStack {
                ListRectShape()
                    .fill(Color.background)
                .frame(width: 368, height: 75)
                Wallet.image
                    .padding(.trailing, 280.0)
                Text("Amazon")
                    .font(.title3)
                    .fontWeight(.light)
                    .foregroundColor(Color.white)
                    .multilineTextAlignment(.leading)
                    .padding(.top, -25.0)
                    .padding(.trailing, 125.0)
                Text("Augest 10 2022")
                    .font(.body)
                    .fontWeight(.light)
                    .foregroundColor(Color.white)
                    .padding(.trailing, 78.0)
                    .padding(.top, 21.0)
                ListRectShape()
                    .fill(Color.background)
                    .frame(width: 83, height: 33)
                    .border(Color.white)
                    .padding(.leading, 225.0)
                Text("$109.08")
                    .foregroundColor(Color.white)
                    .padding(.leading, 225.0)
            }
        
        ZStack {
            ListRectShape()
                .fill(Color.background)
            .frame(width: 368, height: 75)
            Image("Apple WC")
                .padding(.trailing, 280.0)
            Text("Apple")
                .font(.title3)
                .fontWeight(.light)
                .foregroundColor(Color.white)
                .multilineTextAlignment(.leading)
                .padding(.top, -22.0)
                .padding(.trailing, 145.0)
            Text("Augest 10 2022")
                .font(.body)
                .fontWeight(.light)
                .foregroundColor(Color.white)
                .padding(.trailing, 78.0)
                .padding(.top, 21.0)
            ListRectShape()
                .fill(Color.background)
                .frame(width: 83, height: 33)
                .border(Color.white)
                .padding(.leading, 225.0)
            Text("$1000.00")
                .foregroundColor(Color.white)
                .padding(.leading, 225.0)
        }
            
    }
}

struct List_View_Previews: PreviewProvider {
    static var previews: some View {
        List_View(Wallet: Wallet_App[0]) // here is were I'm getting an error
    }
}

//this is my ModelData were I'm trying to load my json file
import Foundation
import Combine
final class ModelData : ObservableObject{
  var Wallet_App: [Wallet_Data] = load("Contents.json")
}

func load<T: Decodable>(_ filename: String) -> T {
    let data: Data
    guard let file = Bundle.main.url(forResource: filename, withExtension: nil)
        else {
            fatalError("Couldn't find (filename) in main bundle.")
    }
    do {
        data = try Data(contentsOf: file)
    } catch {
        fatalError("Couldn't load (filename) from main bundle:\n(error)")
    }
    do {
        let decoder = JSONDecoder()
        return try decoder.decode(T.self, from: data)
    } catch {
        fatalError("Couldn't parse (filename) as (T.self):\n(error)")
    }
}

Wallet_App (BTW, name should start with lowerCase wallet_App, according to Swift conventions), is defined as a property of ModelData. But you call it directly as Wallet_App[0].

You should call:

        List_View(Wallet: ModelData.Wallet_App[0]) 
Accepted Answer

Please use code formatter when you post code. That makes it much easier to read.

import SwiftUI
struct List_View: View {
    var Wallet : Wallet_Data
    var body: some View {
        
            ZStack {
                ListRectShape()
                    .fill(Color.background)
                .frame(width: 368, height: 75)
                Wallet.image
                    .padding(.trailing, 280.0)
                Text("Amazon")
                    .font(.title3)
                    .fontWeight(.light)
                    .foregroundColor(Color.white)
                    .multilineTextAlignment(.leading)
                    .padding(.top, -25.0)
                    .padding(.trailing, 125.0)
                Text("Augest 10 2022")
                    .font(.body)
                    .fontWeight(.light)
                    .foregroundColor(Color.white)
                    .padding(.trailing, 78.0)
                    .padding(.top, 21.0)
                ListRectShape()
                    .fill(Color.background)
                    .frame(width: 83, height: 33)
                    .border(Color.white)
                    .padding(.leading, 225.0)
                Text("$109.08")
                    .foregroundColor(Color.white)
                    .padding(.leading, 225.0)
            }
        
        ZStack {
            ListRectShape()
                .fill(Color.background)
            .frame(width: 368, height: 75)
            Image("Apple WC")
                .padding(.trailing, 280.0)
            Text("Apple")
                .font(.title3)
                .fontWeight(.light)
                .foregroundColor(Color.white)
                .multilineTextAlignment(.leading)
                .padding(.top, -22.0)
                .padding(.trailing, 145.0)
            Text("Augest 10 2022")
                .font(.body)
                .fontWeight(.light)
                .foregroundColor(Color.white)
                .padding(.trailing, 78.0)
                .padding(.top, 21.0)
            ListRectShape()
                .fill(Color.background)
                .frame(width: 83, height: 33)
                .border(Color.white)
                .padding(.leading, 225.0)
            Text("$1000.00")
                .foregroundColor(Color.white)
                .padding(.leading, 225.0)
        }
            
    }
}

struct List_View_Previews: PreviewProvider {
    static var previews: some View {
        List_View(Wallet: Wallet_App[0]) // here is were I'm getting an error
    }
}

//this is my ModelData were I'm trying to load my json file
import Foundation
import Combine
final class ModelData : ObservableObject{
  var Wallet_App: [Wallet_Data] = load("Contents.json")
}

func load<T: Decodable>(_ filename: String) -> T {
    let data: Data
    guard let file = Bundle.main.url(forResource: filename, withExtension: nil)
        else {
            fatalError("Couldn't find (filename) in main bundle.")
    }
    do {
        data = try Data(contentsOf: file)
    } catch {
        fatalError("Couldn't load (filename) from main bundle:\n(error)")
    }
    do {
        let decoder = JSONDecoder()
        return try decoder.decode(T.self, from: data)
    } catch {
        fatalError("Couldn't parse (filename) as (T.self):\n(error)")
    }
}

Wallet_App (BTW, name should start with lowerCase wallet_App, according to Swift conventions), is defined as a property of ModelData. But you call it directly as Wallet_App[0].

You should call:

        List_View(Wallet: ModelData.Wallet_App[0]) 

Sure and Thanks 😇

Value of type ' Module&lt;Wallet_App&gt;' has no subscript please check the below code
 
 
Q