升级到 Xcode 11 Beta 4 后,我在使用时开始看到错误String(format: , args)
with @State
财产。请参阅下面的代码。第二Text
行抛出错误:
表达式类型“String”在没有更多上下文的情况下不明确
while 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
}
}
}
我意识到这是测试版软件,但很好奇是否有人能看到这种行为的原因,或者这只是一个错误。如果这无法解释,我会提交雷达报告。
在 beta 4 中,属性包装器实现略有变化。在 beta 3 中,您的视图被编译器重写为:
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
}
而在 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
}
现在我猜测:这种更改使编译器更难以推断变量的类型?请注意,另一种可行的选择是通过强制转换来帮助编译器一点selection as Int
:
Text("My selection \(String(format: "%02d", selection as Int))")
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)