从发布的值格式化字符串时出现奇怪错误

strange error while formatting a string from published value

在我的模型中格式化发布值(计算结果 属性)我遇到了非常奇怪的错误

class Model: ObservableObject {
    @Published var value = 0.0
    var progress: String {
        String(format: "%.2f %%", value) // Expression type 'String' is ambiguous without more context
    }
}

我找到了 "workaround"

class Model: ObservableObject {
    @Published var value = 0.0
    var progress: String {
        String(format: "%.2f %%", value * 1) // No error :-)
    }
}

有人可以解释这种奇怪的行为吗?

Xcode 11.3.1 解决方案

只需将您的表达式括在括号中,如下所示:

String(format: "%.2f %%", (value))

Xcode 11.4 解

此错误显然已在 Xcode 11.4 中修复!!

无需更改代码。