Playing with swift I found this surprising:
"123".integerValue // <= returns 123
var x = "123"
x.integerValue // <= Error: String does not have a member named integerValue
Can someone explain?
My guess is that in the first example the compiler uses the call to integerValue as additional information to infer the type (choosing between NSString and a Swift String).
In the second example it probably defaults to a Swift String because it doesn't evaluate multiple lines.
I believe this is an example of type inference in action. When you do: "123".integerValue the compiler detects that in this case you want to use an NSString (which it also does when you use string literals as arguments to objective-c functions.
A similar example is:
// x is an Int
let x = 12
// here we tell the compiler that y is a Double explicitly
let y: Double = 12
// z is a Double because 2.5 is a literal Double and so "12" is also
// parsed as a literal Double
let z = 12 * 2.5
Use .toInt()
var x = "123"
var num: Int = x.toInt()
If you do:
var x = "123" as NSString
x.integerValue
var x : NSString = "123" // or this as Sulthan suggests
it won't show that error.
I think your first example is automatically picking up that you want to use a NSString as NSString only has this .integerValue call.
The second example is probably failing because it's not being told what it is and deciding to use a swift string instead.
var x : NSString = "123" would probably be better
var x = "123"; (x as NSString).integerValue // <= returns 123x.bridgeToObjectiveC().integerValue