出现错误:创建字符串文字时,“表达式类型在没有更多上下文的情况下是含糊的”-SwiftUI

时间:2019-09-11 12:51:01

标签: swiftui xcode11

在运行我的代码时,在很多地方出现错误“表达式的类型是模棱两可的,没有更多的上下文”,我不知道为什么。看起来字符串文字不应含糊不清。

我正在运行Xcode 11 GM,当将其强制转换为String时,确实可以使用它,但是在设置Text的颜色时出现错误。这不能通过将其转换为颜色来解决。

`let currentRed:Double = Double.random(in:0 .. <1)

struct S {
  operator auto() const { return 10; } // OK
  template<class T> operator auto() const { return 42; } // error
};

我希望能够设置前景色,但仍会继续出现此错误。这是错误吗?

1 个答案:

答案 0 :(得分:0)

使用以下语法时没有错误:

Text("Red \(currentRed * 255.0)")
    .foregroundColor(Color(red: self.currentRed, green: 0.0, blue: 0.0))

这是输出:

enter image description here

您至少使用了三种类型转换-StringInt,并且无论出现什么叫\(),您只想要最后一个。

如果要显示整数值,只需将行更改为:

Text("Red " + String(Int(currentRed * 255.0)))
    .foregroundColor(Color(red: self.currentRed, green: 0.0, blue: 0.0))

示例输出:

enter image description here

(请记住,由于您要添加随机数,因此输出并不相同!)