Ruvi Ruvi - 1 year ago 86
Swift Question

Xcode 8.0 and Swift 3.0 conversion: Looking for explanation for a particular conversion error

first of all I want to say that I am new to Xcode and Swift and still learning and I am a little bit confused about one conversion error.

I migrated my project form Swift 2.3 to 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 = "???"

At first I thought in Swift 3.0 the cast Type(value) is now forbidden but I checked I get absolutely no compiler warning. Can somebody tell me what the problem with NSNumber(value) is?

As far as I understand value as NSNumber and NSNumber(value) should be the same thing.

Answer Source

In Swift 3, NSNumber(value) won't work. Let's say that your value is an Int. In that case, you'd need NSNUmber(value: yourIntValue). In Swift 3, you must have the name of the first (and in this case the only) parameter in the function call. So, your usage of

value as NSNumber

works, but

NSNumber(value: yourNumberValue)

works too.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download