Something about NSNumberFormatter


#1

Hi there,
I am learning Stanford iOS 8 course in Xcode7, and in the second class I got a problem.
I follow the video and write the following code

return NSNumberFormatter.numberFromString(display.text!)!.doubleValue

But I got an error

Cannot convert value of type ‘String’ to expected argument type ‘NSNumberFOrmatter’

and after searching Google and trying many ways, I change the code to this

let formatter = NSNumberFormatter()
return formatter.numberFromString(display.text!)!.doubleValue

It wroks !

So, my question is what caused this ?

Thank you.


#2

I believe you are missing the initializer call with the ().

It should be:

return NSNumberFormatter().numberFromString(display.text!)!.doubleValue

#3

In Swift 2 (Xcode 7), you can now convert text using the initializers for the types.

var cost = Double(display.text!)