我正在尝试向Float
添加一个扩展名,以转换为具有特定分数数字的String
。这是代码:
extension Float {
func toString(withFractionDigits: Int) -> String {
let nf = NumberFormatter()
nf.numberStyle = .decimal
nf.minimumFractionDigits = withFractionDigits
nf.maximumFractionDigits = withFractionDigits
guard let returnVal = nf.string(from: NSNumber(value: self)) else {
return ""
}
return returnVal
}
}
现在,我在let nf = NumberFormatter()
行中有一个错误,上面写着:
调用中缺少参数标签"编码器:">
当我在网上搜索时,我能找到的只是这条线实际上是正确的。我真的不必插入编码器作为参数。但为什么会失败呢?
提前谢谢。
呵呵。好吧,这很奇怪。我再次打开了该项目,它现在可以工作了。显然,XCode喝醉了。对不起,虚惊一场的家伙。