ios 编译器无法对这个表达式进行类型检查 swift 4?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/52382645/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 09:47:53  来源:igfitidea点击:

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

iosswift

提问by Khodour.F

After updating xCode i am getting this error into my code :

更新 xCode 后,我在我的代码中收到此错误:

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 ?

如何解决这个问题?

回答by Ankit Jayaswal

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

当 swift 编译器发现表达式计算冗长时,会出现此错误。有关更多详细信息,请查看此处

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

回答by Prashant Tukadiya

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.

我已经添加了 Brackets ()但仍然无法正常工作。



To fix this I have added type [String]

为了解决这个问题,我添加了类型 [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

希望对面临同样问题的人有所帮助

回答by Milan Nosá?

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 CGFloatwith an Intit should result in CGFloat, I have explicitly converted the Intto 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).

我注意到我也能够通过提供显式类型转换来修复这种类型的错误(例如,我没有依赖编译器通过将 aCGFloat与a 相乘来推断Int它应该导致CGFloat,而是显式地将 转换IntCGFloat)。似乎问题确实在于类型和自动类型推断和检查。虽然将复杂的表达式分解为更小的部分对于可读性非常有用,但您可以通过明确类型来解决问题(错误消息基本上是说编译器无法进行类型检查 - 因此您可以通过尽可能提供显式类型)。

回答by Glenn

So I have this kind of expression:

所以我有这样的表达:

return (50 + textHeight) + (dateTextHeight + 16) + (5 + 8) + (16 + 20)

I knew I had to breakdown or make this expression shorter to get rid of the error. Like perhaps:

我知道我必须分解或缩短这个表达式才能消除错误。也许像:

return (50 + textHeight) + (dateTextHeight + 16) + 49

Although true enough it helped the compiler to do its job, the main cause of the error is the optional Int textHeight. I think no matter how long your expression, it should be compile-able.

虽然它确实帮助编译器完成其工作,但错误的主要原因是可选的 Int textHeight。我认为无论你的表达有多长,它都应该是可编译的。

回答by gbarnett

I had this error when I accidentally mixed in an optional in an expression. After force unwrapping the error went away.

当我不小心在表达式中混入了一个可选项时,我遇到了这个错误。强制展开后,错误消失了。

let a : String = "A" 
let b : String = "B" 
let c = letterDictionary["C"]

let imageName : String = a + b + c //error 
let imageName : String = a + b + c! //no error 

回答by Snips

I see this issue quite regularly when working with SwiftUI, and it's always been the result of a syntax error. I'm currently using Xcode 11.4.1, and it was true for earlier versions supporting SwiftUI.

在使用 SwiftUI 时,我经常看到这个问题,而且它一直是语法错误的结果。我目前使用的是 Xcode 11.4.1,对于支持 SwiftUI 的早期版本也是如此。

For example, I just burned 50 mins with this error because I'd changed the name of a function parameter in another file. The compiler reported this error in a SwiftUI file that was completely untouched, except it invoked the function without the changed parameter name. Rather than explicitly pointing out the parameter name mismatch is baulked with this error.

例如,由于我更改了另一个文件中的函数参数的名称,因此我刚刚因此错误而烧毁了 50 分钟。编译器在一个完全没有改动的 SwiftUI 文件中报告了这个错误,除了它在没有更改参数名称的情况下调用了函数。而不是明确指出参数名称不匹配是因为这个错误。

回答by Gope

This is indeed a memory problem. It happened to me, when I only 350 mb of free memory on my 16GB machine. Using f.e. CleanMyMac -> Free up memory resolved this problem.

这确实是内存问题。它发生在我身上,当时我的 16GB 机器上只有 350 mb 的可用内存。使用 fe CleanMyMac -> 释放内存解决了这个问题。