mikegrep
mikegrep

Reputation: 121

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

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
            }
        }
    }

Upvotes: 0

Views: 812

Answers (1)

Duncan C
Duncan C

Reputation: 131398

It looks to me like the compiler can't figure out the type that results from the expression representedElements + cells.flatMap { <closure> }. Try adding an explicit type on that returned value:

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

(It's hard to know what's going on since we don'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.)

Upvotes: 1

Related Questions