簡體   English   中英

如何解決氣旋復雜性? - RxDataSource RxSwift - SwiftLint

[英]How to fix Cyclonemtic Complexity? - RxDataSource RxSwift - SwiftLint

當我使用 RxDataSource 時,我遇到了這樣的警告:“違反循環復雜度:Function 的復雜度應為 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):
}

在此處輸入圖像描述

誰能幫我解決這個問題? 非常感謝。

此處的解決方案是不要為您的單元格項目使用枚舉。 一個可能的解決方案如下:

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

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

每個 DisplayableItem 都被賦予了制作 UITableViewCell 的方法。 你可以用上面的閉包,或者用一個協議和一堆子類來做到這一點。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM