Swift 5编译错误:编译器无法在合理时间内对此表达式进行类型检查。

huangapple go评论65阅读模式
英文:

Swift 5 compile error The compiler is unable to type-check this expression in reasonable time

问题

你好,我只会提供代码的翻译:

// 错误位于此返回表达式 ` |representedElements + cells.flatMap { group -> [UICollectionViewLayoutAttributes] in`!编译器无法在合理的时间内对此表达式进行类型检查;尝试将表达式分解为不同的子表达式

return representedElements + cells.flatMap { group -> [UICollectionViewLayoutAttributes] in
    guard let section = group.first?.indexPath.section else {
        return group
    }
    let evaluatedSectionInset = evaluatedSectionInsetForSection(at: section)
    let evaluatedMinimumInteritemSpacing = evaluatedMinimumInteritemSpacingForSection(at: section)
    var origin = (collectionView.bounds.width + evaluatedSectionInset.left - evaluatedSectionInset.right - group.reduce(0, { $0 + $1.frame.size.width }) - CGFloat(group.count - 1) * evaluatedMinimumInteritemSpacing) / 2
    // 重新定位组中的每个元素
    return group.map {
        $0.frame.origin.x = origin
        origin += $0.frame.size.width + evaluatedMinimumInteritemSpacing
        return $0
    }
}

希望这能帮到你。

英文:

Hello I am not Swift developer I just clone repository from GitHub and it was working before I update the Xcode version and Swift.
I am here for help this is the code and I receive build error that says

> The compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions

is located at couple of places I I just don't know how to fix it.I read online that the problem is with too difficult expression of the code and many operation at once.The code need to be simplified but idk how to do it on swift.Any help would be welcome repository is dead and the owner was not online for a year.

Error is in this return expression |representedElements + cells.flatMap { group -> [UICollectionViewLayoutAttributes] in !x The compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions

 return representedElements + cells.flatMap { group -> [UICollectionViewLayoutAttributes] in
            guard let section = group.first?.indexPath.section else {
                return group
            }
            let evaluatedSectionInset = evaluatedSectionInsetForSection(at: section)
            let evaluatedMinimumInteritemSpacing = evaluatedMinimumInteritemSpacingForSection(at: section)
            var origin = (collectionView.bounds.width + evaluatedSectionInset.left - evaluatedSectionInset.right - group.reduce(0, { $0 + $1.frame.size.width }) - CGFloat(group.count - 1) * evaluatedMinimumInteritemSpacing) / 2
            // we reposition each element of a group
            return group.map {
                $0.frame.origin.x = origin
                origin += $0.frame.size.width + evaluatedMinimumInteritemSpacing
                return $0
            }
        }
    } else {
        for layoutAttributes in layoutAttributesForElements {
            guard layoutAttributes.representedElementKind == nil else {
                representedElements.append(layoutAttributes)
                continue
            }
            // copying is required to avoid "UICollectionViewFlowLayout cache mismatched frame"
            let currentItemAttributes = layoutAttributes.copy() as! UICollectionViewLayoutAttributes
            // if the current frame, once stretched to the full column doesn't intersect the previous frame then they are on different columns
            if previousFrame != nil && !currentItemAttributes.frame.intersects(CGRect(x: previousFrame!.origin.x, y: -.greatestFiniteMagnitude, width: previousFrame!.size.width, height: .infinity)) {
                cells.append([])
            }
            cells[cells.endIndex - 1].append(currentItemAttributes)
            previousFrame = currentItemAttributes.frame
        }
        // we reposition all elements
        return representedElements + cells.flatMap { group -> [UICollectionViewLayoutAttributes] in
            guard let section = group.first?.indexPath.section else {
                return group
            }
            let evaluatedSectionInset = evaluatedSectionInsetForSection(at: section)
            let evaluatedMinimumInteritemSpacing = evaluatedMinimumInteritemSpacingForSection(at: section)
            var origin = (collectionView.bounds.height + evaluatedSectionInset.top - evaluatedSectionInset.bottom - group.reduce(0, { $0 + $1.frame.size.height }) - CGFloat(group.count - 1) * evaluatedMinimumInteritemSpacing) / 2
            // we reposition each element of a group
            return group.map {
                $0.frame.origin.y = origin
                origin += $0.frame.size.height + evaluatedMinimumInteritemSpacing
                return $0
            }
        }
    }

答案1

得分: 1

看起来编译器无法确定表达式 `representedElements + cells.flatMap { <closure> }` 的结果类型。尝试在返回的值上添加显式类型:

return Array(
representedElements + cells.flatMap { group ->
[UICollectionViewLayoutAttributes] in

}
}


<details>
<summary>英文:</summary>

It looks to me like the compiler can&#39;t figure out the type that results from the expression `representedElements + cells.flatMap { &lt;closure&gt; }`.  Try adding an explicit type on that returned value:


return Array<UICollectionViewLayoutAttributes> (
representedElements + cells.flatMap { group ->
[UICollectionViewLayoutAttributes] in
<your closure code here>
}
}


(It&#39;s hard to know what&#39;s going on since we don&#39;t know the type of the variables `representedElements`, `group`, or `cells`. I had to take a guess as to the correct type for the array that code is supposed to return.)


</details>



huangapple
  • 本文由 发表于 2023年5月7日 20:26:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76193922.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定