我在 ContentView 的末尾声明了 Double 的扩展。但它显示错误“Initializeer 'init(_:) 要求 'Double' 符合 'StringProtocol'”。
struct ContentView : View {
@State var demo: Double = 0
var body: some View {
VStack {
Slider(value: $demo, from: 0.0, through: 100.0, by: 0.01)
.padding()
Text(demo.roundTo(places: 5))
}
}
}
extension Double {
public func roundTo(places: Int) -> Double {
let divisor = pow(10.0, Double(places))
return (self * divisor).rounded() / divisor
}
}