Reputation: 33
While running my code I get the error 'Type of expression is ambiguous without more context' in a number of places and I don't know why. It seems like a string literal should not be ambiguous.
I'm running Xcode 11 GM and I do get it to work when I cast it to a String but then I get an error when setting the color for the Text. This is not solved by casting it to a color.
`let currentRed: Double = Double.random(in: 0..<1)
var body: some View {
return Text(String("Red \(Int(currentRed * 255.0))")) // <- Returns Error without cast to String
.foregroundColor(Color(red: self.currentRed, green: 0.0, blue: 0.0)) // <- Returns same error }
I expect to be able to set the foregroundColor but I keep on getting this error. Is this a bug?
Upvotes: 0
Views: 399
Reputation:
I have no errors when I use this syntax:
Text("Red \(currentRed * 255.0)")
.foregroundColor(Color(red: self.currentRed, green: 0.0, blue: 0.0))
And here's the output:
You're using at least three type conversions - String
, Int
, and whatever you call \()
where it appears you just want the last one.
If you wish to display the integer value, just change your line to:
Text("Red " + String(Int(currentRed * 255.0)))
.foregroundColor(Color(red: self.currentRed, green: 0.0, blue: 0.0))
Sample output:
(Keep in mind, since you are adding a random number, the output isn't identical!)
Upvotes: 0