无法调整已删除的类别总数
Unable to Adjust Deleted Category Totals
我有一个用户可以创建和删除的 5 个费用类别的列表。删除用户类别时,我需要从总计和所有用户类别的总计中减去类别总计。此外,删除类别之后的任何用户类别都必须将总数在 totalArray 中向下移动一个位置。在 C 中,这将是一个简单的操作,但在 SwiftUI 中,我不知道从哪里开始。我可以使用索引来判断我要删除的类别吗?
错误包括“无法将 'IndexSet.type' 类型的值转换为预期的参数类型 'Int'”,以及“'Int' 类型的表达式模式无法匹配 'IndexSet' 类型的值]
假设我定义了所有 5 个类别。如果我删除类别 #2,我需要将类别 3 - 5 的总数向下移动一位到 2 - 4 的总数。显示用户类别总计时,categories.catItem.count
用于检查每个类别总计(第一、二、三类等)的有效性。
// this definition is found in different file (Class UserData)
@Published var totalArray = [Double] ()
...
struct manCatView: View {
@EnvironmentObject var userData: UserData
@EnvironmentObject var categories: Categories
var body: some View {
List {
ForEach(categories.catItem) { item in
if item.noShow == false {
HStack {
Image(systemName: item.catPix).resizable()
.frame(width: 30, height: 30)
Spacer()
Text(item.catName)
}
}
}
.onDelete(perform: removeItems)
}
.navigationBarTitle(Text("Manage Categories"), displayMode: .inline)
...
func removeItems(偏移量:IndexSet){
// at this point the deleted category still exists
// 1. must subtract 'to be deleted' category total from userTotal and grandTotal
if userData.totalArray[IndexSet] != 0 {
userData.totalArray[grandTotal] -= userData.totalArray[IndexSet]
userData.totalArray[userTotal] -= userData.totalArray[IndexSet]
}
// 2. must shift up category totals following the deleted category
switch offsets
{
case 0: // 1st category being deleted
case 1: // 2nd category being deleted
case 2: // 3rd category being deleted
case 3: // 4th category being deleted
case 4: // 5th category being deleted
default:
}
// 3. delete category
categories.catItem.remove(atOffsets: offsets)
}
我不确定这是否更好。我将类别总计移到了存储结构中。除了删除类别外,还需要先调整合计。当用户单击删除时,removeItems 函数指针指向要删除的类别。您可能认为获取类别总数 (categories.catItem[].catTotal) 很容易,但我收到错误“无法将类型 'IndexSet' 的值转换为预期的参数类型 Int”。我认为 Asperi 希望我尝试某种映射,但我不确定从哪里开始。
struct CatItem: Codable, Identifiable {
var id = UUID()
var catNum: Int // entryView category pointer
var catName: String // category name
var catTotal: Double // category total
var catPix: String // sf symbol
var catShow: Bool // don't show certain categories
}
class Categories: ObservableObject {
@Published var catItem: [CatItem] {
}
}
...
struct manCatView: View {
@EnvironmentObject var userData: UserData
@EnvironmentObject var categories: Categories
var pic: String = ""
var body: some View {
List {
ForEach(categories.catItem) { index in
if index.catShow == true {
HStack {
let pic = categories.catItem[index].catPix
Image(systemName: pic)
.resizable()
.foregroundColor(Color(colors[index]))
.frame(width: 40, height: 40)
Text("")
.frame(width: 40, alignment: .leading)
Text(index.catName)
}
}
} // end foreach
.onDelete(perform: removeItems)
} // end list
.navigationBarTitle(Text("Manage Categories"), displayMode: .inline)
}
func removeItems(at offsets: IndexSet) {
//subtract category amount from system totals.
let catTotal = categories.catItem[offsets].catTotal // <-- error here
userData.totalArray[grandTotal] -= catTotal
userData.totalArray[userTotal] -= catTotal
categories.catItem.remove(atOffsets: offsets)
}
}
我有一个用户可以创建和删除的 5 个费用类别的列表。删除用户类别时,我需要从总计和所有用户类别的总计中减去类别总计。此外,删除类别之后的任何用户类别都必须将总数在 totalArray 中向下移动一个位置。在 C 中,这将是一个简单的操作,但在 SwiftUI 中,我不知道从哪里开始。我可以使用索引来判断我要删除的类别吗?
错误包括“无法将 'IndexSet.type' 类型的值转换为预期的参数类型 'Int'”,以及“'Int' 类型的表达式模式无法匹配 'IndexSet' 类型的值]
假设我定义了所有 5 个类别。如果我删除类别 #2,我需要将类别 3 - 5 的总数向下移动一位到 2 - 4 的总数。显示用户类别总计时,categories.catItem.count
用于检查每个类别总计(第一、二、三类等)的有效性。
// this definition is found in different file (Class UserData)
@Published var totalArray = [Double] ()
...
struct manCatView: View {
@EnvironmentObject var userData: UserData
@EnvironmentObject var categories: Categories
var body: some View {
List {
ForEach(categories.catItem) { item in
if item.noShow == false {
HStack {
Image(systemName: item.catPix).resizable()
.frame(width: 30, height: 30)
Spacer()
Text(item.catName)
}
}
}
.onDelete(perform: removeItems)
}
.navigationBarTitle(Text("Manage Categories"), displayMode: .inline)
...
func removeItems(偏移量:IndexSet){
// at this point the deleted category still exists
// 1. must subtract 'to be deleted' category total from userTotal and grandTotal
if userData.totalArray[IndexSet] != 0 {
userData.totalArray[grandTotal] -= userData.totalArray[IndexSet]
userData.totalArray[userTotal] -= userData.totalArray[IndexSet]
}
// 2. must shift up category totals following the deleted category
switch offsets
{
case 0: // 1st category being deleted
case 1: // 2nd category being deleted
case 2: // 3rd category being deleted
case 3: // 4th category being deleted
case 4: // 5th category being deleted
default:
}
// 3. delete category
categories.catItem.remove(atOffsets: offsets)
}
我不确定这是否更好。我将类别总计移到了存储结构中。除了删除类别外,还需要先调整合计。当用户单击删除时,removeItems 函数指针指向要删除的类别。您可能认为获取类别总数 (categories.catItem[].catTotal) 很容易,但我收到错误“无法将类型 'IndexSet' 的值转换为预期的参数类型 Int”。我认为 Asperi 希望我尝试某种映射,但我不确定从哪里开始。
struct CatItem: Codable, Identifiable {
var id = UUID()
var catNum: Int // entryView category pointer
var catName: String // category name
var catTotal: Double // category total
var catPix: String // sf symbol
var catShow: Bool // don't show certain categories
}
class Categories: ObservableObject {
@Published var catItem: [CatItem] {
}
}
...
struct manCatView: View {
@EnvironmentObject var userData: UserData
@EnvironmentObject var categories: Categories
var pic: String = ""
var body: some View {
List {
ForEach(categories.catItem) { index in
if index.catShow == true {
HStack {
let pic = categories.catItem[index].catPix
Image(systemName: pic)
.resizable()
.foregroundColor(Color(colors[index]))
.frame(width: 40, height: 40)
Text("")
.frame(width: 40, alignment: .leading)
Text(index.catName)
}
}
} // end foreach
.onDelete(perform: removeItems)
} // end list
.navigationBarTitle(Text("Manage Categories"), displayMode: .inline)
}
func removeItems(at offsets: IndexSet) {
//subtract category amount from system totals.
let catTotal = categories.catItem[offsets].catTotal // <-- error here
userData.totalArray[grandTotal] -= catTotal
userData.totalArray[userTotal] -= catTotal
categories.catItem.remove(atOffsets: offsets)
}
}