问题描述
我不熟悉编码,一直在尝试使用SwiftUI构建我的第一个iPhone应用程序,因此请原谅我的问题和代码中的任何错误。 我已经成功地从json文件中解析了数据,并且能够在2个列表视图和最终的详细视图中显示数据。我现在正在尝试过滤3个列表,以使汽车的品牌不在第一个列表中重复,相应汽车的型号不在第二个列表中重复,并且相应汽车的徽章也不再重复第三个列表。Make screenshot,Model screenshot,Detail screenshot。
如果问题过于笼统,我深表歉意,并且我很欣赏可能有很多方法可以完成我想做的事情。我也很高兴能为所建议的任何资源指明正确的方向,以便我可以更好地理解这种数据管理类型。
谢谢。
JSON代码段
[
{
"id": 1,"make": "Alfa Romeo","model": "147","badge": "Twin Spark","series": null,"year": 2004,"fuel": "Petrol","driveType": "2WD","bodyType": "Small Hatch","transmission": "Automatic","manualDeduction": -250,"subtraction1": null,"subtraction1Difference": 0,"add1": "Sunroof","add1Difference": 250,"add2": null,"add2Difference": 0,"add3": null,"add3Difference": 0,"add4": null,"add4Differnece": 0,"avgAnnualKms": 15000,"age": 16,"averageKmsForYear": 240000,"priceWhenNew": 0,"depreciationForKms": 0,"algorithmValuation": 0,"valuerValuation": 500,"valo": 500,"logoName": "Alfa"
},{
"id": 2,"model": "156","badge": "JTS","bodyType": "Sedan","valuerValuation": 1000,"valo": 1000,
数据:
import Foundation
func JsonParse() -> [VehicleData]{
guard let url = Bundle.main.url(forResource: "vehicleValuationData",withExtension: "json")
else {
fatalError("Failed to locate in bundle")
}
guard let data = try? Data(contentsOf: url)
else {
fatalError("Failed to locate in bundle")
}
guard let loaded = try? JSONDecoder().decode([VehicleData].self,from: data) else {
fatalError("Failed to decode from bundle")
}
return loaded
}
struct VehicleData: Codable,Identifiable{
let id: Int
let make: String
let model,badge: String
let series: String?
let year: Int
let fuel: String
let driveType: String
let bodyType: String
let transmission: String
let manualDeduction: Int
let subtraction1: String?
let subtraction1Difference: Int
let add1: String?
let add1Difference: Int
let add2: String?
let add2Difference: Int
let add3: String?
let add3Difference: Int
let add4: String?
let add4Differnece,avgAnnualKms,age,averageKmsForYear: Int
let priceWhenNew: Int
let depreciationForKms,algorithmValuation: Double
let valuerValuation: Int
let valo: Double
let logoName: String
}
MakeCarView视图。
import SwiftUI
struct MakeCarView: View {
var body: some View {
NavigationView{
List(JsonParse()) { car in
MakeCellRow(car: car)
}.navigationTitle("Make")
}
}
}
struct MakeCellRow: View {
var car: VehicleData
var body: some View {
HStack {
NavigationLink(destination: ModelView()) {
Image(car.logoName)
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 60,height: 60,alignment: .center)
.padding()
Text(car.make)
.font(.title2)
}
.background(Color("card color"))
.cornerRadius(15)
.shadow(color: Color.black.opacity(0.25),radius: 3,x: 0,y: 0)
}
}
}
struct MakeCarViewsPreview: PreviewProvider {
static var previews: some View {
Group {
MakeCarView()
}
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)