文字字符串与swift中的`String`

时间:2022-11-24 11:12:32

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?

谁能解释一下?

4 个解决方案

#1


6  

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).

我的猜测是,在第一个例子中,编译器使用对integerValue的调用作为推断类型的附加信息(在NSString和Swift String之间进行选择)。

In the second example it probably defaults to a Swift String because it doesn't evaluate multiple lines.

在第二个示例中,它可能默认为Swift String,因为它不会评估多行。

#2


1  

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.

我相信这是行动中类型推断的一个例子。当你执行:“123”.integerValue编译器检测到在这种情况下你想要使用NSString(当你使用字符串文字作为objective-c函数的参数时它也会这样做。

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

#3


1  

Use .toInt()

var x = "123"

var num: Int = x.toInt()

#4


0  

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.

我认为你的第一个例子是自动获取你想要使用NSString,因为NSString只有这个.integerValue调用。

The second example is probably failing because it's not being told what it is and deciding to use a swift string instead.

第二个例子可能是失败的,因为它没有被告知它是什么,并决定使用swift字符串代替。

#1


6  

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).

我的猜测是,在第一个例子中,编译器使用对integerValue的调用作为推断类型的附加信息(在NSString和Swift String之间进行选择)。

In the second example it probably defaults to a Swift String because it doesn't evaluate multiple lines.

在第二个示例中,它可能默认为Swift String,因为它不会评估多行。

#2


1  

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.

我相信这是行动中类型推断的一个例子。当你执行:“123”.integerValue编译器检测到在这种情况下你想要使用NSString(当你使用字符串文字作为objective-c函数的参数时它也会这样做。

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

#3


1  

Use .toInt()

var x = "123"

var num: Int = x.toInt()

#4


0  

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.

我认为你的第一个例子是自动获取你想要使用NSString,因为NSString只有这个.integerValue调用。

The second example is probably failing because it's not being told what it is and deciding to use a swift string instead.

第二个例子可能是失败的,因为它没有被告知它是什么,并决定使用swift字符串代替。