Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix failed to locate file json in Swift

Tags:

swiftui

I have a little problem, i try to decode my json code, helping me with tutorial from https://www.hackingwithswift.com/quick-start/swiftui/swiftui-tutorial-building-a-complete-project, but my problem is that my function does not detect my file, my error is "Fatal error: Failed to locate data.json in bundle", if anyone has a solution thank you in advance

// File decode Json

import UIKit

extension Bundle {
    func decode<T: Decodable>(_ type: T.Type, from file: String) -> T {

        guard let url = self.url(forResource: file, withExtension: nil) else {
            fatalError("Failed to locate \(file) in bundle.")
        }

        guard let data = try? Data(contentsOf: url) else {
            fatalError("Failed to load \(file) from bundle.")
        }

        let decoder = JSONDecoder()

        guard let loaded = try? decoder.decode(T.self, from: data) else {
            fatalError("Failed to decode \(file) from bundle.")
        }
        return loaded
    }
}
// File Model

import SwiftUI

struct CharacterModel: Codable, Hashable, Equatable, Identifiable{

    var id:Int
    var name:String
    var force:Int
    var spirituel:Int
    var endurance:Int
    var defensePhysique:Int
    var defenseMagique:Int
    var vitesse:Int

    var mainImage: String {
        name.replacingOccurrences(of: " ", with: "-").lowercased()
    }

    var thumbnailImage: String {
        "\(mainImage)-thumb"
    }

    #if DEBUG
    static let exemple = CharacterModel(
        id: 1,
        name: "Hippo",
        force: 100,
        spirituel: 10,
        endurance: 10,
        defensePhysique: 100,
        defenseMagique: 100,
        vitesse: 10
    )
    #endif

}
// My data.json
[
    {
        "id": 1,
        "name": "Hippo",
        "imageName": "Hippo",
        "Endurance": 100,
        "force": 10,
        "Spirituel": 10,
        "DefensePhysique": 100,
        "DefenseMagique": 100,
        "Vitesse": 10
    }
]
import SwiftUI

struct ContentView: View {
    let character = Bundle.main.decode([CharacterModel].self, from: "data.json")

    var body: some View {
        NavigationView {
            List{
                ForEach(character) { characters in
                    Image(characters.name)
                }
            }
        .navigationBarTitle("Menu")
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
like image 389
Novice-Dev Avatar asked Dec 15 '25 04:12

Novice-Dev


1 Answers

The file appears to be missing from your project's bundled resources. Open the Build Phases tab in your project file search for data.json. If nothing comes up, then navigate to theCopy Bundle Resources section and add the file using the + button located at the bottom of the section.

enter image description here

like image 168
jfuellert Avatar answered Dec 16 '25 21:12

jfuellert



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!