我对转换错误有点困惑。
我将我的项目从 Swift 2.3 迁移到 Swift 3.0
func updateCelsiusLabel() {
if let value = celsiusValue {
//This was the original code (that worked but is) failing after migration
//due to: Argument labels do not match any available overloads
celsiusLabel.text = numberFormatter.string(from: NSNumber(value))
//This is my code trying to fix this issue and the project is now compiling
//and everything is fine
celsiusLabel.text = numberFormatter.string(from: value as NSNumber)
}
else { celsiusLabel.text = "???"
}
}
起初我以为在 Swift 3.0 中,强制转换 Type(value) 现在是被禁止的,但我检查了一下,我完全没有收到编译器警告。有人能告诉我 NSNumber(value) 的问题是什么吗?
据我了解 value as NSNumber 和 NSNumber(value) 应该是同一件事。
最佳答案
在 Swift 3 中,NSNumber(value) 将不起作用。假设您的值(value)是一个 Int。在这种情况下,您需要 NSNUber(value: yourIntValue)。在 Swift 3 中,您必须在函数调用中拥有第一个(在本例中是唯一的)参数的名称。所以,你对
value as NSNumber
有效,但是
NSNumber(value: yourNumberValue)
也有效。
关于swift - Xcode 8.0 和 Swift 3.0 转换 : Looking for explanation for a particular conversion error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39548698/