The compiler is unable to type-check this expression swift 4?
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
Just try to break up the expression to several simpler subexpression. E.g.:
let halfOfViewWidth = cutOutViewWidth.constant / 2
let textWidthAndPadding = options.textWidth + (padding * 2)
let spaceFromRightSide = cutOutViewX.constant + halfOfViewWidth + (textWidthAndPadding / 2)
EDIT
I noticed that I was able to fix this type of error also by providing explicit type conversions (e.g., instead of relying on the compiler to infer that by multiplying a CGFloat
with an Int
it should result in CGFloat
, I have explicitly converted the Int
to CGFloat
). Seems that the problem lies indeed in types and automatic type inference and checking. While breaking up the the complex expression to smaller parts can be very useful to readability, you can solve the problem by being explicit about the types (the error message basically says that the compiler is unable do the type check - thus you can help it by providing explicit types where-ever possible).
I have faced similar issue with different scenario
I was trying to create array of string
let selectedData = [
(data?.nose?.stuffyNose) ?? "",
(data?.nose?.sinusProblems) ?? "",
(data?.nose?.hayFever) ?? "",
(data?.nose?.sneezingAttacks) ?? "",
(data?.nose?.excessiveMucusFormation) ?? ""
]
I have already addd Brackets () but still was not working.
To fix this I have added type [String]
let selectedData:[String] = [
(data?.nose?.stuffyNose) ?? "",
(data?.nose?.sinusProblems) ?? "",
(data?.nose?.hayFever) ?? "",
(data?.nose?.sneezingAttacks) ?? "",
(data?.nose?.excessiveMucusFormation) ?? ""
]
Hope it is helpful to someone facing same issue