Search code examples
iosswift

The compiler is unable to type-check this expression Swift 4?


After updating Xcode I am getting this error into my code:

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

The code :

//check popup in window frame

let spaceFromLeftSide = cutOutViewX.constant + cutOutViewWidth.constant/2 - (options.textWidth + padding*2)/2

if spaceFromLeftSide < 0{
    
    if options.side == .bottom {
        messageRightSpaceFromBottomDot.constant -= spaceFromLeftSide - padding
    }
    else if options.side == .top{
        messageRightSpaceFromTopDot.constant += spaceFromLeftSide - padding
    }
}

let spaceFromRightSide = cutOutViewX.constant + cutOutViewWidth.constant/2 + (options.textWidth + padding*2)/2

if spaceFromRightSide > targetView.frame.size.width{
    
    if options.side == .bottom {
        messageRightSpaceFromBottomDot.constant -= spaceFromRightSide - ( targetView.frame.size.width )
    }
    else if options.side == .top{
        messageRightSpaceFromTopDot.constant += spaceFromRightSide - ( targetView.frame.size.width )
    }
}

Error in line :

let spaceFromRightSide = cutOutViewX.constant + cutOutViewWidth.constant/2 + (options.textWidth + padding*2)/2

How to fix this?


Solution

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

    This error appears when swift compiler finds the expression calculation lengthy. For more details check here

    To resolve this, you just need to break your expression into smaller parts. Just like:

    let cutOutxOrigin = 3 * cutOutViewX.constant / 2
    let actualPadding = (options.textWidth + padding * 2) / 2
    
    let spaceFromRightSide = cutOutxOrigin + actualPadding