0

当我使用 RxDataSource 时,我遇到了这样的警告“违反循环复杂度:函数的复杂度应为 10 或更低:当前复杂度等于 14 (cyclomatic_complexity)”。

我的代码结构是这样的:

struct ItemDetailDataSource {
    typealias DataSource = RxTableViewSectionedReloadDataSource
    
    static func dataSource() -> DataSource<ItemDetailTableViewSection> {
        return .init(configureCell: { (dataSource, tableView, indexPath, _) -> UITableViewCell in
            
            switch dataSource[indexPath] {
            case .itemInfoTopItem(let info):
                guard let cell = tableView.dequeueReusableCell(withIdentifier: ConstantsForCell.infoTopTableViewCell,
                                                               for: indexPath)
                    as? InfoTopTableViewCell else {
                        return UITableViewCell()
                }
                cell.configure(info)
                return cell
            case .itemHintItem(let hint):
            ...
            case .itemManaColdownItem(let manacd):
            case .itemNotesItem(let notes):
            case .itemAttribItem(let attrib):
            case .itemLoreItem(let lore):
            case .itemComponentsItem(let components):
}

在此处输入图像描述

谁能帮我解决这个问题?非常感谢。

4

1 回答 1

1

此处的解决方案是不要为您的单元格项目使用枚举。一个可能的解决方案如下:

struct DisplayableItem {
    let makeCell: (UITableView, IndexPath) -> UITableViewCell
}

struct ItemDetailDataSource {
    typealias DataSource = RxTableViewSectionedReloadDataSource
    
    static func dataSource() -> DataSource<ItemDetailTableViewSection> {
        .init { _, tableView, indexPath, item in
            item.makeCell(tableView, indexPath)
        }
    }
}

每个 DisplayableItem 都被赋予了制作 UITableViewCell 的方法。你可以用上面的闭包,或者用一个协议和一堆子类来做到这一点。

于 2021-12-08T17:58:17.813 回答