简体   繁体   English

在 Xcode11 Beta 4 中将 String(format: , args) 与 SwiftUI 一起使用时出错

[英]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.升级到 Xcode 11 Beta 4 后,我在将String(format: , args)@State属性一起使用时开始看到错误。 See code below.请参阅下面的代码。 Second Text line throws an error:第二个Text行引发错误:

Expression type 'String' is ambiguous without more context没有更多上下文的表达式类型“字符串”是不明确的

while Text s 1, 3, and 4 work just fine.Text s 1、3 和 4 工作得很好。

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.我意识到这是 Beta 版软件,但很好奇是否有人可以看到这种行为的原因,或者这只是一个错误。 If this can't be explained, I'll file a radar.如果这无法解释,我将提交雷达。

In beta 4, the property wrapper implementation changed slightly.在 beta 4 中,属性包装器实现略有变化。 In beta 3, your View was rewritten by the compiler as:在 beta 3 中,您的 View 被编译器重写为:

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:而在 Beta 4 上,它会这样做:

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 :请注意,另一个有效的替代方法是通过将selection as Int来帮助编译器:

Text("My selection \(String(format: "%02d", selection as Int))")

Update (Xcode 11.2)更新(Xcode 11.2)

I also get the error:我也收到错误:

'inout Path' is not convertible to '@lvalue Path'

with this code:使用此代码:

struct ContentView : View {
    @State var selection = 2

    var body: some View {
        VStack {
            Text(String(format: "%d", selection)) // does not work
        }
    }
}

Solved by and adding the $ prefix and then accessing wrappedValue in String(format:, args:) :解决并添加$前缀,然后访问String(format:, args:) wrappedValue

struct ContentView : View {
    @State var selection = 2

    var body: some View {
        VStack {
            Text(String(format: "%d", $selection.wrappedValue)) // works
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM