如何将 RandomAccessCollection 与 ForEach 列表一起使用?
How can I use a RandomAccessCollection with a ForEach list?
我有一个 class 看起来像这样:
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 class 中更改什么才能正常工作?
如果您正确地声明了您的 Transaction
class(顺便说一句,这可能与 SwiftUI Transaction
类型冲突,所以我建议重命名您的类型),那么一切都需要(至少用 Xcode 11.4 测试过)是使用不同的 ForEach
构造函数:
ForEach(transactions, id: \.self) { transaction in
EmptyView()
}
更新: 这是一个交易 class 我用它测试了你的代码快照(正如我提到的,我重命名它以避免与内置类型冲突)
class ATransaction: Equatable, Identifiable, Hashable {
static func == (lhs: ATransaction, rhs: ATransaction) -> Bool {
lhs.id == rhs.id
}
func hash(into hasher: inout Hasher) {
hasher.combine(id)
}
let id = UUID()
}
我有一个 class 看起来像这样:
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 class 中更改什么才能正常工作?
如果您正确地声明了您的 Transaction
class(顺便说一句,这可能与 SwiftUI Transaction
类型冲突,所以我建议重命名您的类型),那么一切都需要(至少用 Xcode 11.4 测试过)是使用不同的 ForEach
构造函数:
ForEach(transactions, id: \.self) { transaction in
EmptyView()
}
更新: 这是一个交易 class 我用它测试了你的代码快照(正如我提到的,我重命名它以避免与内置类型冲突)
class ATransaction: Equatable, Identifiable, Hashable {
static func == (lhs: ATransaction, rhs: ATransaction) -> Bool {
lhs.id == rhs.id
}
func hash(into hasher: inout Hasher) {
hasher.combine(id)
}
let id = UUID()
}