ForEach 循环遍历视图模型数组导致 Xcode 编译错误的问题 - SwiftUI
Issues with ForEach Loop over view model array causing Xcode compile error - SwiftUI
当我尝试使用主视图模型的子视图模型添加 ForEach 循环时出现未知编译错误:
我以前在类似情况下看到过这个错误,我想知道 ForEach 循环中的 ViewModels 是否存在任何可能导致此错误的常见错误?
这是我的带有 ForEach 循环的 SwiftUI 视图:
struct GroupHubView: View {
@ObservedObject var groupHubVM: GroupHubViewModel
var body: some View {
NavigationView {
ScrollView {
VStack {
ForEach(groupHubVM.activeGroupViewModels) { groupCellVM in
GroupCellView(groupCellVM: groupCellVM)
}
}
}
}
这是 ViewModel,在我调用 loadActiveGroups()
:
之前,它有一个组合填充的子视图模型列表,开始时是一个空数组
class GroupHubViewModel: ObservableObject {
@Published var groupRepository: GroupStoreType
@Published var currentUser: CurrentUserType
@Published var activeGroupViewModels: [GroupCellViewModel] = [GroupCellViewModel]()
@Published var pendingInviteViewModels: [GroupCellViewModel] = [GroupCellViewModel]()
private var cancellables = Set<AnyCancellable>()
init(groupRepository: GroupStoreType, currentUser: CurrentUserType = CurrentUserProfile.shared) {
self.groupRepository = groupRepository
self.currentUser = currentUser
self.loadActiveGroups()
self.loadPendingGroups()
}
func loadActiveGroups() {
self.groupRepository.accountabilityGroupsPublisher.map { groups in
groups.filter { group in
if group.members?.contains(where: { [=12=].userId == self.currentUser.currentUser!.id && [=12=].membershipStatus == .active }) == true {
return true
} else {
return false
}
}
.map { group in
GroupCellViewModel(groupRepository: self.groupRepository, accountabilityGroup: group)
}
}
.assign(to: \.activeGroupViewModels, on: self)
.store(in: &cancellables)
}
错误:
只要我添加这个:
ForEach(groupHubVM.activeGroupViewModels) { groupCellVM in
我在视图中收到了完整的未知编译错误。
如果我只是添加 ForEach,如下所示:ForEach(groupHubVM.activeGroupViewModels)
,我会收到此错误消息,指出“无法推断出“通用参数“Content””:
已编辑:
此时我的 GroupCellViewModel 非常简单 - 它只包含 GroupRepository 和此时的特定组:
class GroupCellViewModel: ObservableObject {
@Published var groupRepository: GroupStoreType
@Published var group: AccountabilityGroup
private var cancellables = Set<AnyCancellable>()
init(groupRepository: GroupStoreType, currentUser: CurrentUserType = CurrentUserProfile.shared, accountabilityGroup: AccountabilityGroup) {
self.groupRepository = groupRepository
self.group = accountabilityGroup
}
}
根据 loreipsum 和 George 的上述评论,我的问题是 GroupCellViewModel 不符合 Identifiable。我解决了这个问题并解决了这个问题:
class GroupCellViewModel: ObservableObject, Identifiable {
@Published var groupRepository: GroupStoreType
@Published var group: AccountabilityGroup
private var cancellables = Set<AnyCancellable>()
init(groupRepository: GroupStoreType, currentUser: CurrentUserType = CurrentUserProfile.shared, accountabilityGroup: AccountabilityGroup) {
self.groupRepository = groupRepository
self.group = accountabilityGroup
}
}
当我尝试使用主视图模型的子视图模型添加 ForEach 循环时出现未知编译错误:
我以前在类似情况下看到过这个错误,我想知道 ForEach 循环中的 ViewModels 是否存在任何可能导致此错误的常见错误?
这是我的带有 ForEach 循环的 SwiftUI 视图:
struct GroupHubView: View {
@ObservedObject var groupHubVM: GroupHubViewModel
var body: some View {
NavigationView {
ScrollView {
VStack {
ForEach(groupHubVM.activeGroupViewModels) { groupCellVM in
GroupCellView(groupCellVM: groupCellVM)
}
}
}
}
这是 ViewModel,在我调用 loadActiveGroups()
:
class GroupHubViewModel: ObservableObject {
@Published var groupRepository: GroupStoreType
@Published var currentUser: CurrentUserType
@Published var activeGroupViewModels: [GroupCellViewModel] = [GroupCellViewModel]()
@Published var pendingInviteViewModels: [GroupCellViewModel] = [GroupCellViewModel]()
private var cancellables = Set<AnyCancellable>()
init(groupRepository: GroupStoreType, currentUser: CurrentUserType = CurrentUserProfile.shared) {
self.groupRepository = groupRepository
self.currentUser = currentUser
self.loadActiveGroups()
self.loadPendingGroups()
}
func loadActiveGroups() {
self.groupRepository.accountabilityGroupsPublisher.map { groups in
groups.filter { group in
if group.members?.contains(where: { [=12=].userId == self.currentUser.currentUser!.id && [=12=].membershipStatus == .active }) == true {
return true
} else {
return false
}
}
.map { group in
GroupCellViewModel(groupRepository: self.groupRepository, accountabilityGroup: group)
}
}
.assign(to: \.activeGroupViewModels, on: self)
.store(in: &cancellables)
}
错误:
只要我添加这个:
ForEach(groupHubVM.activeGroupViewModels) { groupCellVM in
我在视图中收到了完整的未知编译错误。
如果我只是添加 ForEach,如下所示:ForEach(groupHubVM.activeGroupViewModels)
,我会收到此错误消息,指出“无法推断出“通用参数“Content””:
已编辑: 此时我的 GroupCellViewModel 非常简单 - 它只包含 GroupRepository 和此时的特定组:
class GroupCellViewModel: ObservableObject {
@Published var groupRepository: GroupStoreType
@Published var group: AccountabilityGroup
private var cancellables = Set<AnyCancellable>()
init(groupRepository: GroupStoreType, currentUser: CurrentUserType = CurrentUserProfile.shared, accountabilityGroup: AccountabilityGroup) {
self.groupRepository = groupRepository
self.group = accountabilityGroup
}
}
根据 loreipsum 和 George 的上述评论,我的问题是 GroupCellViewModel 不符合 Identifiable。我解决了这个问题并解决了这个问题:
class GroupCellViewModel: ObservableObject, Identifiable {
@Published var groupRepository: GroupStoreType
@Published var group: AccountabilityGroup
private var cancellables = Set<AnyCancellable>()
init(groupRepository: GroupStoreType, currentUser: CurrentUserType = CurrentUserProfile.shared, accountabilityGroup: AccountabilityGroup) {
self.groupRepository = groupRepository
self.group = accountabilityGroup
}
}