Convert Int to String in Swift

时间:2022-06-03 16:02:29

I'm trying to work out how to cast an Int into a String in Swift.

我正在尝试研究如何在Swift中将Int转换为String。

I figure out a workaround, using NSNumber but I'd love to figure out how to do it all in Swift.

我找到了一个解决方法,使用NSNumber,但我很想知道如何在Swift中完成所有操作。

let x : Int = 45let xNSNumber = x as NSNumberlet xString : String = xNSNumber.stringValue

19 个解决方案

#1


812  

Converting Int to String:

将Int转换为String:

let x : Int = 42var myString = String(x)

And the other way around - converting String to Int:

反过来 - 将String转换为Int:

let myString : String = "42"let x: Int? = myString.toInt()if (x != nil) {    // Successfully converted String to Int}

Or if you're using Swift 2 or 3:

或者,如果您使用的是Swift 2或3:

let x: Int? = Int(myString)

#2


82  

Check the Below Answer:

检查下面的答案:

let x : Int = 45var stringValue = "\(x)"print(stringValue)

#3


51  

In Swift 3.0:

在Swift 3.0中:

var value: Int = 10var string = String(describing: value)

#4


47  

Here are 4 methods:

这里有4种方法:

var x = 34var s = String(x)var ss = "\(x)"var sss = toString(x)var ssss = x.description

I can imagine that some people will have an issue with ss. But if you were looking to build a string containing other content then why not.

我可以想象有些人会遇到ss的问题。但是如果你想构建一个包含其他内容的字符串,那么为什么不呢。

#5


21  

Just for completeness, you can also use:

为了完整起见,您还可以使用:

let x = 10.description

or any other value that supports a description.

或任何其他支持描述的值。

#6


20  

Swift 4:

let x:Int = 45let str:String = String(describing: x)

Convert Int to String in SwiftDeveloper.Apple.com > String > init(describing:)

Developer.Apple.com> String> init(描述:)

#7


8  

To save yourself time and hassle in the future you can make an Int extension. Typically I create a shared code file where I put extensions, enums, and other fun stuff. Here is what the extension code looks like:

为了节省您自己的时间和麻烦,您可以进行Int扩展。通常我会创建一个共享代码文件,我可以在其中放置扩展,枚举和其他有趣的东西。这是扩展代码的样子:

extension Int{    func toString() -> String    {        var myString = String(self)        return myString    }}

Then later when you want to convert an int to a string you can just do something like:

然后,当您想要将int转换为字符串时,您可以执行以下操作:

var myNumber = 0var myNumberAsString = myNumber.toString()

#8


7  

for whatever reason the accepted answer did not work for me. I went with this approach:

无论出于何种原因,接受的答案对我都不起作用。我采用这种方法:

var myInt:Int = 10var myString:String = toString(myInt)

#9


7  

in swift 3.0 this is how we can convert Int to String and String to Int

在swift 3.0中,我们可以将Int转换为String,将String转换为Int

//convert Integer to String in Swift 3.0let theIntegerValue :Int = 123  // this can be var alsolet theStringValue :String = String(theIntegerValue)//convert String to Integere in Swift 3.0let stringValue : String = "123"let integerValue : Int = Int(stringValue)!

#10


5  

Multiple ways to do this :

多种方法:

var str1:String="\(23)"var str2:String=String(format:"%d",234)

#11


4  

Swift 2:

var num1 = 4var numString = "56"var sum2 = String(num1) + numStringvar sum3 = Int(numString)

#12


4  

Swift 4:

Trying to show the value in label without Optional() word.

尝试在没有Optional()字的情况下显示标签中的值。

here x is a Int value using.

这里x是使用的Int值。

let str:String = String(x ?? 0)

#13


2  

iam using this simple approach

我使用这种简单的方法

String to Int:

字符串到Int:

 var a = Int()var string1 = String("1")a = string1.toInt()

and from Int to String:

从Int到String:

var a = Int()a = 1var string1 = String() string1= "\(a)"

#14


2  

Convert Unicode Int to String

For those who want to convert an Int to a Unicode string, you can do the following:

对于想要将Int转换为Unicode字符串的用户,可以执行以下操作:

let myInteger: Int = 97// convert Int to a valid UnicodeScalarguard let myUnicodeScalar = UnicodeScalar(myInteger) else {    return ""}// convert UnicodeScalar to Stringlet myString = String(myUnicodeScalar)// resultsprint(myString) // a

Or alternatively:

let myInteger: Int = 97if let myUnicodeScalar = UnicodeScalar(myInteger) {    let myString = String(myUnicodeScalar)}

#15


1  

let a =123456888var str = String(a)

OR

var str = a as! String

#16


0  

exampleLabel.text = String(yourInt)

exampleLabel.text = String(yourInt)

#17


0  

To convert String into Int

将String转换为Int

var numberA = Int("10")Print(numberA) // It will print 10

To covert Int into String

将Int转换为String

var numberA = 10

1st way)

print("numberA is \(numberA)") // It will print 10

2nd way)

var strSomeNumber = String(numberA)

or

var strSomeNumber = "\(numberA)"

#18


0  

In swift 3.0, you may change integer to string as given below

在swift 3.0中,您可以将整数更改为字符串,如下所示

let a:String = String(stringInterpolationSegment: 15)

Another way is

另一种方式是

let number: Int = 15let _numberInStringFormate: String = String(number)

//or any integer number in place of 15

//或任何整数来代替15

#19


0  

let Str = "12"let num: Int = 0num = Int (str)

#1


812  

Converting Int to String:

将Int转换为String:

let x : Int = 42var myString = String(x)

And the other way around - converting String to Int:

反过来 - 将String转换为Int:

let myString : String = "42"let x: Int? = myString.toInt()if (x != nil) {    // Successfully converted String to Int}

Or if you're using Swift 2 or 3:

或者,如果您使用的是Swift 2或3:

let x: Int? = Int(myString)

#2


82  

Check the Below Answer:

检查下面的答案:

let x : Int = 45var stringValue = "\(x)"print(stringValue)

#3


51  

In Swift 3.0:

在Swift 3.0中:

var value: Int = 10var string = String(describing: value)

#4


47  

Here are 4 methods:

这里有4种方法:

var x = 34var s = String(x)var ss = "\(x)"var sss = toString(x)var ssss = x.description

I can imagine that some people will have an issue with ss. But if you were looking to build a string containing other content then why not.

我可以想象有些人会遇到ss的问题。但是如果你想构建一个包含其他内容的字符串,那么为什么不呢。

#5


21  

Just for completeness, you can also use:

为了完整起见,您还可以使用:

let x = 10.description

or any other value that supports a description.

或任何其他支持描述的值。

#6


20  

Swift 4:

let x:Int = 45let str:String = String(describing: x)

Convert Int to String in SwiftDeveloper.Apple.com > String > init(describing:)

Developer.Apple.com> String> init(描述:)

#7


8  

To save yourself time and hassle in the future you can make an Int extension. Typically I create a shared code file where I put extensions, enums, and other fun stuff. Here is what the extension code looks like:

为了节省您自己的时间和麻烦,您可以进行Int扩展。通常我会创建一个共享代码文件,我可以在其中放置扩展,枚举和其他有趣的东西。这是扩展代码的样子:

extension Int{    func toString() -> String    {        var myString = String(self)        return myString    }}

Then later when you want to convert an int to a string you can just do something like:

然后,当您想要将int转换为字符串时,您可以执行以下操作:

var myNumber = 0var myNumberAsString = myNumber.toString()

#8


7  

for whatever reason the accepted answer did not work for me. I went with this approach:

无论出于何种原因,接受的答案对我都不起作用。我采用这种方法:

var myInt:Int = 10var myString:String = toString(myInt)

#9


7  

in swift 3.0 this is how we can convert Int to String and String to Int

在swift 3.0中,我们可以将Int转换为String,将String转换为Int

//convert Integer to String in Swift 3.0let theIntegerValue :Int = 123  // this can be var alsolet theStringValue :String = String(theIntegerValue)//convert String to Integere in Swift 3.0let stringValue : String = "123"let integerValue : Int = Int(stringValue)!

#10


5  

Multiple ways to do this :

多种方法:

var str1:String="\(23)"var str2:String=String(format:"%d",234)

#11


4  

Swift 2:

var num1 = 4var numString = "56"var sum2 = String(num1) + numStringvar sum3 = Int(numString)

#12


4  

Swift 4:

Trying to show the value in label without Optional() word.

尝试在没有Optional()字的情况下显示标签中的值。

here x is a Int value using.

这里x是使用的Int值。

let str:String = String(x ?? 0)

#13


2  

iam using this simple approach

我使用这种简单的方法

String to Int:

字符串到Int:

 var a = Int()var string1 = String("1")a = string1.toInt()

and from Int to String:

从Int到String:

var a = Int()a = 1var string1 = String() string1= "\(a)"

#14


2  

Convert Unicode Int to String

For those who want to convert an Int to a Unicode string, you can do the following:

对于想要将Int转换为Unicode字符串的用户,可以执行以下操作:

let myInteger: Int = 97// convert Int to a valid UnicodeScalarguard let myUnicodeScalar = UnicodeScalar(myInteger) else {    return ""}// convert UnicodeScalar to Stringlet myString = String(myUnicodeScalar)// resultsprint(myString) // a

Or alternatively:

let myInteger: Int = 97if let myUnicodeScalar = UnicodeScalar(myInteger) {    let myString = String(myUnicodeScalar)}

#15


1  

let a =123456888var str = String(a)

OR

var str = a as! String

#16


0  

exampleLabel.text = String(yourInt)

exampleLabel.text = String(yourInt)

#17


0  

To convert String into Int

将String转换为Int

var numberA = Int("10")Print(numberA) // It will print 10

To covert Int into String

将Int转换为String

var numberA = 10

1st way)

print("numberA is \(numberA)") // It will print 10

2nd way)

var strSomeNumber = String(numberA)

or

var strSomeNumber = "\(numberA)"

#18


0  

In swift 3.0, you may change integer to string as given below

在swift 3.0中,您可以将整数更改为字符串,如下所示

let a:String = String(stringInterpolationSegment: 15)

Another way is

另一种方式是

let number: Int = 15let _numberInStringFormate: String = String(number)

//or any integer number in place of 15

//或任何整数来代替15

#19


0  

let Str = "12"let num: Int = 0num = Int (str)