Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error using String(format: , args) with SwiftUI in Xcode11 Beta 4

After upgrading to Xcode 11 Beta 4, I starting seeing an error when using String(format: , args) with @State property. See code below. Second Text line throws an error:

Expression type 'String' is ambiguous without more context

while Texts 1, 3, and 4 work just fine.

struct ContentView : View {
    @State var selection = 2

    var body: some View {
        VStack {
            Text("My selection \(selection)") // works
            Text("My selection \(String(format: "%02d", selection))") // error
            Text("My selection \(String(format: "%02d", Int(selection)))") // works
            Text("My selection \(String(format: "%02d", $selection.binding.value))") // works
        }
    }
}

I realize this is Beta software, but was curious if anyone can see a reason for this behavior or is this simply a bug. If this can't be explained, I'll file a radar.

like image 695
Kon Avatar asked May 17 '26 05:05

Kon


1 Answers

In beta 4, the property wrapper implementation changed slightly. In beta 3, your View was rewritten by the compiler as:

internal struct ContentView : View {
  @State internal var selection: Int { get nonmutating set }
  internal var $selection: Binding<Int> { get }
  @_hasInitialValue private var $$selection: State<Int>
  internal var body: some View { get }
  internal init(selection: Int = 2)
  internal init()
  internal typealias Body = some View
}

while on Beta 4, it does this:

internal struct ContentView : View {
  @State @_projectedValueProperty($selection) internal var selection: Int { get nonmutating set }
  internal var $selection: Binding<Int> { get }
  @_hasInitialValue private var _selection: State<Int>
  internal var body: some View { get }
  internal init(selection: Int = 2)
  internal init()
  internal typealias Body = some View
}

Now I am guessing: this change makes it more difficult for the compiler to infer the type of your variable? Note that another alternative that does work, is helping the compiler a little, by casting selection as Int:

Text("My selection \(String(format: "%02d", selection as Int))")
like image 84
kontiki Avatar answered May 21 '26 15:05

kontiki



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!