Код: Выделить всё
[
{
"day": "Monday",
"locationName": "Memorial Union Quad",
"coordinate": [38.54141, -121.74845],
"menu": {
"Penne Pasta w/ Bolognese" : ["🌾","🐷"],
"Penne Pasta w/ Roasted Mushrooms" : ["🌾","V"]
}
},
{
"day": "Tuesday",
"locationName": "International Center",
"coordinate": [38.54540, -121.75494],
"menu": {
"Beef & Lamb Gyro" : ["🌾","🫛", "🥛"],
"Edamame Hummus w/ Veggies" : ["🫛","🥛", "SE", "VE"]
}
},
{
"day": "Wednesday",
"locationName": "Storer Hall",
"coordinate": [38.54114, -121.75461],
"menu": {
"Seafood Salad Tostada" : ["🥚","🦐", "🫛", "🐟"],
"Southwest Black Bean Tostada" : ["V"]
}
},
{
"day": "Thursday",
"locationName": "Orchard Park",
"coordinate": [38.544828, -121.765170],
"menu": {
"Teriyaki Beef w/ Stir Fry Noodles" : ["🌾","🫛", "SE"],
"Teriyaki Tofu w/ Veggie Stir Fry" : ["🌾","🫛", "SE","V"]
}
},
{
"day": "Friday",
"locationName": "Memorial Union Quad",
"coordinate": [38.54141, -121.74845],
"menu": {
"Soy Ciltrano Lime Chicken" : ["🌾","🫛"],
"Southwest Tofu" : ["🫛","V"]
}
}
]
Код: Выделить всё
import Foundation
import OrderedCollections
struct Menu : Codable, Hashable {
var id: UUID { UUID() }
let day: String
let locationName: String
let coordinate: [Double]
let menu: OrderedDictionary
func getTodaysLocation(_ today: String)-> String{
if today == day{
return locationName
}
return ""
}
}
Код: Выделить всё
import Foundation
extension Bundle {
func decode(_ file: String) -> [Menu] {
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()
do {
return try decoder.decode([Menu].self, from: data)
} catch DecodingError.keyNotFound(let key, let context) {
fatalError("Failed to decode \(file) from bundle due to missing key '\(key.stringValue)' – \(context.debugDescription)")
} catch DecodingError.typeMismatch(_, let context) {
fatalError("Failed to decode \(file) from bundle due to type mismatch – \(context.debugDescription)")
} catch DecodingError.valueNotFound(let type, let context) {
fatalError("Failed to decode \(file) from bundle due to missing \(type) value – \(context.debugDescription)")
} catch DecodingError.dataCorrupted(_) {
fatalError("Failed to decode \(file) from bundle because it appears to be invalid JSON.")
} catch {
fatalError("Failed to decode \(file) from bundle: \(error.localizedDescription)")
}
}
}
Правильно ли я моделирую данные? Или это проблема с функцией декодирования в пакете?
Я пытался изменить способ моделирования данных, но я новичок в JSON, поэтому не совсем уверен, проблема в том, как я смоделировал JSON в Swift.
Подробнее здесь: https://stackoverflow.com/questions/792 ... -dictionar