升级到Xcode 11 Beta 4之后,在将String(format: , args)
与@State
属性一起使用时,我开始看到错误。请参见下面的代码。 Text
第二行引发错误:
表达式类型'String'不明确,没有更多上下文
同时Text
的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版软件,但很好奇是否有人可以看到此行为的原因,或者仅仅是一个错误。如果无法解释,我将提交雷达报告。
答案 0 :(得分:4)
在beta 4中,属性包装器的实现略有变化。在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
}
在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))")
答案 1 :(得分:0)
更新(Xcode 11.2)
我也收到错误消息:
struct ContentView : View {
@State var selection = 2
var body: some View {
VStack {
Text(String(format: "%d", selection)) // does not work
}
}
}
使用以下代码:
$
通过添加wrappedValue
前缀然后在String(format:, args:)
中访问struct ContentView : View {
@State var selection = 2
var body: some View {
VStack {
Text(String(format: "%d", $selection.wrappedValue)) // works
}
}
}
来解决:
SocketConfig socketConfig = SocketConfig.custom().setSoKeepAlive(true).setSoTimeout(360000000).build(); //We need to set socket keep alive
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(360000000).build();
CloseableHttpClient client = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).
setDefaultSocketConfig(socketConfig).build();