Getting error " Thread 1: Fatal error: Could not decode sanantoniotx.json from bundle."
Here is my code.
// MARK: - Welcome
struct Welcome: Codable {
var status: String
var urgentAdsHTML, data: [AdInfo]
}
// MARK: - AdInfo
struct AdInfo: Codable {
var aid, bname, bcity: String?
var bstate: Bstate?
var distance: String?
var cangap: String?
var vip: Int?
var photo: [String: String]?
var adsTitle, adsMsg, status: String?
var urgentAdsHTML: [AdInfo]?
enum CodingKeys: String, CodingKey {
case aid, bname, bcity, bstate, distance, cangap, vip, photo
case adsTitle
case adsMsg
case status, urgentAdsHTML
}
static let allAds: [AdInfo] = Bundle.main.decode(file: "sanantoniotx.json")
static let sampleAd: AdInfo = allAds[0]
}
enum Bstate: String, Codable {
case tx = "TX"
}
extension Bundle {
func decode<T: Decodable>(file: String) -> T {
guard let url = self.url(forResource: file, withExtension: nil) else {
fatalError("Could not find \(file) in bundle.")
}
guard let data = try? Data(contentsOf: url) else {
fatalError("Could not load \(file) from bundle.")
}
let decoder = JSONDecoder()
guard let loadedData = try? decoder.decode(T.self, from: data) else {
fatalError("Could not decode \(file) from bundle.")
}
return loadedData
}
}
receiving the error at.
fatalError("Could not decode \(file) from bundle.")
Id be very thankful if anyone can help.
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
here is my code.
extension Bundle {
func decode<T: Decodable>(file: String) -> T {
guard let url = self.url(forResource: file, withExtension: nil) else {
fatalError("Could not find \(file) in bundle.")
}
guard let data = try? Data(contentsOf: url) else {
fatalError("Could not load \(file) from bundle.")
}
let decoder = JSONDecoder()
guard let loadedData = try? decoder.decode(T.self, from: data) else {
fatalError("Could not decode \(file) from bundle.")
}
return loadedData
}
}
I am having issues on these lines.
guard let data = try? Data(contentsOf: url) else {
fatalError("Could not load \(file) from bundle.")
}
Someone please help. Thank you!
getting the error " No exact matches in call to instance method 'appendInterpolation' "
HStack {
ForEach(ads, id: \.bcity) { adn in
Text("Store")
Text("\(adn.bcity)")
Text("- 3 miles")
}
}
getting the error on the second Text. Please help been trying for awhile to fix
Im having an issue with my View. When ever i want to put it inside of a ScrollView there is extra padding that appears. I tested to find the cause of it but to no luck. Here is the code for it :
NavigationView{
NavigationLink(destination: HiringAdPage(favorite: true)) {
HiringAds()
}
.buttonStyle(PlainButtonStyle())
}
the ScrollPart of it :
ScrollView{
VStack(spacing: 0) {
ForEach(0..<5) {index in
HiringAdsView()
}
}
}
Someone please help been trying for a while now..