我有一个看起来像这样的类:
public class TransactionResults: Identifiable {
public typealias CustomClassValue = Transaction
public typealias CustomClassIndex = Array<CustomClassValue>.Index
public let id = UUID()
private let transactions = sampleTransactions
}
extension TransactionResults: RandomAccessCollection {
public var startIndex: CustomClassIndex { return transactions.startIndex }
public var endIndex: CustomClassIndex { return transactions.endIndex }
public subscript(index: CustomClassIndex) -> CustomClassValue? {
get { if index >= 0 && index < transactions.count {
return transactions[index]
} else {
return nil
}
}
}
}
当我尝试将它与 ForEach 一起使用时,出现错误:Cannot convert value of type 'TransactionResults' to expected argument type 'Range<Int>'
struct TransactionListView: View {
@State private var transactions = TransactionResults()
var body: some View {
NavigationView {
List() {
ForEach(transactions) { transaction in
EmptyView()
}
}
}
}
}
我需要在我的 TransactionResults 类中更改什么才能正常工作?