lochiwei
lochiwei

Reputation: 1358

Expression type `String` is ambiguous without more context?

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())

compiler error

Upvotes: 0

Views: 165

Answers (2)

vacawama
vacawama

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

Jawad Ali
Jawad Ali

Reputation: 14417

There is no issue with your code... restart Xcode

enter image description here

Upvotes: 1

Related Questions