【问题标题】:Issues with ForEach Loop over view model array causing Xcode compile error - SwiftUI视图模型数组上的 ForEach 循环问题导致 Xcode 编译错误 - SwiftUI
【发布时间】:2021-09-04 14:38:07
【问题描述】:

当我尝试使用我的主视图模型的子视图模型添加 ForEach 循环时遇到未知的编译错误:

我之前在类似的情况下看到过这个错误,我想知道 ForEach 循环中的 ViewModel 是否存在任何可能导致此错误的常见错误?

这是我的带有 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: { $0.userId == self.currentUser.currentUser!.id && $0.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),我会收到此错误消息,指出“无法推断通用参数“内容”:

编辑: 我的 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
    }
    
}

【问题讨论】:

  • 第一个你错过了, id:\.id第二个告诉你足够
  • @loremipsum 如果符合Identifiable,则推断id: \.id
  • 看看GroupCellViewModel 会很有用。如果你能创建一个minimal reproducible example,那就更好了。
  • 啊,我很沮丧这就是问题所在 - 我没有符合可识别的 GroupCellViewModel。我刚刚添加了该协议一致性,它似乎正在工作。谢谢你们。

标签: xcode foreach swiftui viewmodel


【解决方案1】:

根据 loreipsum 和 George 的上述 cmets,我的问题是 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
    }
    
}

【讨论】:

    猜你喜欢
    • 2015-06-16
    • 1970-01-01
    • 1970-01-01
    • 2020-05-18
    • 2013-08-03
    • 1970-01-01
    • 1970-01-01
    • 2015-10-30
    • 1970-01-01
    相关资源
    最近更新 更多