I'm using two loops, one nested in another to create and assign a subclass to the parent class. I'm using x for one loop and y for the other loop. Print statement shows the loop variables being updated correctly, however the subclass has both variables as the same value. I'm not sure if the value is being stored as the same value or being displayed as the same value. I've checked both creation and display code. I can't find the error.
var body: some View {
NavigationSplitView {
List {
ForEach(routes) { item in
NavigationLink {
Text("Route: \(item.route_name) \nSeason: \(item.route_season)\nCoordinates: \(item.route_coordinates)\nProcessed: \(String(item.route_processed))\nNumber of baseboards: \(item.route_baseboards.count)")
} label: {
Text(item.route_name)
} //end route nav link
} //end route for each
.onDelete(perform: deleteItems) //end route nav
ForEach(baseboards) { item in
NavigationLink {
//if let test = item.baseboard_heightPoints.count {
Text("Grid Size: \(String(item.baseboard_grid_size))\nRow:\(String(item.baseboard_row))\nColumn:\(String(item.baseboard_column))\nHeight Point Count:")//\(String(item.baseboard_heightPoints.flatMap { $0 } .count))") //fix count later
//Text("Test")
// Text("test2")
// } else {
// Text("Grid Size: \(String(item.baseboard_grid_size))\nRow:\(String(item.baseboard_row))\nColumn:\(String(item.baseboard_column))\nHeight Point Count: 0")
// }
} label: {
Text(String(item.baseboard_grid_size))
}}
.onDelete(perform: deleteItemsBaseboards) //end baseboard route nav
// ForEach(heightPoints) { item in NavigationLink {
// Text("Row:\(String(item.hp_row))\nColumn:\(String(item.hp_column))\nElevation:\(String(item.hp_elevation))")
// } label: {
// Text("Row:\(String(item.hp_row))\nColumn:\(String(item.hp_column))")
// }}
//.onDelete(perform: deleteItemsBaseboards)
}
.toolbar {
ToolbarItem {
Button(action: addItem) {
Label("Add Route", systemImage: "plus")
}
//Button(action: addItem) { //not showing up
// Label("Add Baseboard", systemImage: "arrow.up")
//}
}
} //end toolbar
} detail: {
Text("Select a route")
} //end NavigationSplitView
} //end view body
private func addItem() {
/*withAnimation { */
let newItem = Route(
route_name: "test route " + UUID().uuidString,
route_season: "summer",
route_processed: false,
route_coordinates: "Somewhere",
route_region: "US",
route_baseboards: [])
modelContext.insert(newItem) //end add route
//add baseboards to each route
let bb_StartCol = 0
let bb_EndCol = 3
let bb_StartRow = 0
let bb_EndRow = 3
//let grid5m = 148
//let grid10m = 76
//let gridHD = 5760
var bb_grid_size = 5760
let bb_sectionsVerticle = 180
let bb_sectionsHorizonal = 360
var sectionData: Data
var dataInputArray: [UInt8] = [0x03, 0x00, 0x00, 0x00, 0x00, 0x82, 0xFF, 0x3F, 0x00, 0x00, 0x00, 0x00, 0x20, 0xF8, 0xFF]
sectionData = Data(withUnsafeBytes(of: dataInputArray, Array.init))
for x in bb_StartCol...bb_EndCol { //columns
for y in bb_StartRow...bb_EndRow { //rows
print(x,y)
if bb_grid_size < 1000 {
let newItem2 = Baseboard(
baseboard_column: Int16(x),
baseboard_row: Int16(y),
texture: "Grid",
baseboard_processed: false,
baseboard_grid_size: Int16(bb_grid_size),//(x+2)+(y+2),
baseboard_heightPoints: Array(repeating: Array(repeating: 0, count: bb_grid_size), count: bb_grid_size),
baseboard_HPSection: [],
baseboard_route: newItem
//baseboard_hps: []
)
modelContext.insert(newItem2) //insert baseboard for each run
} else {
let newItem2 = Baseboard(
baseboard_column: Int16(x),
baseboard_row: Int16(y),
texture: "Grid",
baseboard_processed: false,
baseboard_grid_size: Int16(bb_grid_size),//(x+2)+(y+2),
baseboard_heightPoints: [],
baseboard_HPSection: Array(repeating: Array(repeating: sectionData, count: bb_sectionsVerticle), count: bb_sectionsHorizonal),
baseboard_route: newItem
//baseboard_hps: []
)
modelContext.insert(newItem2) //insert baseboard for each run
}
//print(x,y)
} //end y for loop - baseboards
} //end x for loop - baseboards
// } // end animation of adding new items
} // end function add items
private func deleteItems(offsets: IndexSet) {
withAnimation {
for index in offsets {
modelContext.delete(routes[index])
}
}
}
private func deleteItemsBaseboards(offsets: IndexSet) {
withAnimation {
for index in offsets {
modelContext.delete(baseboards[index])
}
}
}
//private func deleteItemsHeightPoints(offsets: IndexSet) {
// withAnimation {
// for index in offsets {
// modelContext.delete(heightPoints[index])
// }
// }
// }
}
#Preview {
ContentView()
.modelContainer(for: Route.self, inMemory: false)
}
The error turned out to be in the item file. I didn’t create the variable correctly in the initialization. I referenced the other variable, instead of the same variable.
I found the easiest way to find the SwiftData Database is to search for defaul.store. I then found the right directory among the results. Did info on the fil, pulled the path as path. Then started terminal, then SQLite3, and opened the path and file name.