Reputation: 1358
The following code in the Swift Playgrounds app for iPad gives me an error message:
Expression type `String` is ambiguous without more context
Does anyone know why this is happening? Thanks in advance.
import SwiftUI
import Foundation
import PlaygroundSupport
struct ContentView: View {
@State private var progress: Double = 0.6
var body: some View {
VStack {
Text("test")
HStack {
Slider(value: $progress)
Text(String(format: "%.2f", progress))
}
}.border(Color.blue)
}
}
PlaygroundPage.current.setLiveView(ContentView())
Upvotes: 0
Views: 165
Reputation: 154671
Yes, that is odd. For some reason the Swift Playgrounds on the iPad is different.
Use String interpolation with a specifier instead:
Replace
Text(String(format: "%.2f", progress))
With
Text("\(progress, specifier: "%.2f")")
Note:
If you don't want your text jumping around, use a font with mono spaced digits such as Helvetica Neue. Add .font(Font.custom("Helvetica Neue", size: 20))
to the end of your Text
statement.
Upvotes: 1