Konwertuj Int na String w Swift

Próbuję rozpracować jak wrzucić {[1] } do String W Swift.

Wymyśliłem obejście, używając NSNumber, ale chciałbym dowiedzieć się, jak to wszystko zrobić w Swift.

let x : Int = 45
let xNSNumber = x as NSNumber
let xString : String = xNSNumber.stringValue
Author: Mike Chamberlain, 2014-06-11

19 answers

Konwersja Int na String:

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

I odwrotnie-konwersja String na Int:

let myString : String = "42"
let x: Int? = myString.toInt()

if (x != nil) {
    // Successfully converted String to Int
}

Lub jeśli używasz Swift 2 lub 3:

let x: Int? = Int(myString)
 812
Author: Shai,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-02-10 12:44:30

Sprawdź poniższą odpowiedź:

let x : Int = 45
var stringValue = "\(x)"
print(stringValue)
 82
Author: PREMKUMAR,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-02-03 15:18:02

W Swift 3.0:

var value: Int = 10
var string = String(describing: value)
 51
Author: vladiulianbogdan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-04-10 09:36:36

Oto 4 metody:

var x = 34
var s = String(x)
var ss = "\(x)"
var sss = toString(x)
var ssss = x.description
Mogę sobie wyobrazić, że niektórzy ludzie będą mieli problem z ss. Ale jeśli chcesz zbudować ciąg zawierający inne treści, to dlaczego nie.
 47
Author: Ian Bradbury,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-01-28 22:09:53

Tylko dla kompletności, możesz również użyć:

let x = 10.description

Lub jakąkolwiek inną wartość obsługującą opis.

 21
Author: Mike Lischke,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-12-30 15:44:29

Swift 4:

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

Tutaj wpisz opis obrazka Developer.Apple.com > String > INIT (opisujący:)

 20
Author: Hamed Gh,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-08-29 13:02:20

Aby zaoszczędzić sobie czasu i kłopotów w przyszłości można zrobić rozszerzenie Int. Zazwyczaj tworzę plik udostępnionego kodu, w którym umieszczam rozszerzenia, Liczby i inne zabawne rzeczy. Oto jak wygląda kod rozszerzenia:

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

Później, gdy chcesz przekonwertować int na ciąg znaków, możesz po prostu zrobić coś takiego:

var myNumber = 0
var myNumberAsString = myNumber.toString()
 8
Author: user2266987,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-03-17 15:34:04

Z jakiegokolwiek powodu zaakceptowana odpowiedź nie zadziałała dla mnie. Wybrałem takie podejście:

var myInt:Int = 10
var myString:String = toString(myInt)
 7
Author: bkopp,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-12-14 09:10:18

W swift 3.0 Tak możemy przekształcić Int na String i String na Int

//convert Integer to String in Swift 3.0

let theIntegerValue :Int = 123  // this can be var also
let theStringValue :String = String(theIntegerValue)


//convert String to Integere in Swift 3.0


let stringValue : String = "123"
let integerValue : Int = Int(stringValue)!
 7
Author: Anuradh S,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-09-11 06:52:25

Wiele sposobów, aby to zrobić:

var str1:String="\(23)"
var str2:String=String(format:"%d",234)
 5
Author: Dhruv Ramani,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-02-05 22:37:05

Swift 2:

var num1 = 4
var numString = "56"
var sum2 = String(num1) + numString
var sum3 = Int(numString)
 4
Author: juanmorillios,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-09-15 18:54:05

Swift 4:

Próba pokazania wartości w etykiecie bez opcjonalnego () słowa.

Tutaj x jest wartością Int używając.

let str:String = String(x ?? 0)
 4
Author: Harshil Kotecha,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-03-31 10:21:31

Iam używając tego prostego podejścia

String to Int:

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

I od Int do String:

var a = Int()
a = 1
var string1 = String()
 string1= "\(a)"
 2
Author: Muraree Pareek,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-04-13 06:28:15

Konwertuj Unicode Int na String

Dla tych, którzy chcą przekonwertować Int na ciąg Unicode, możesz wykonać następujące czynności:

let myInteger: Int = 97

// convert Int to a valid UnicodeScalar
guard let myUnicodeScalar = UnicodeScalar(myInteger) else {
    return ""
}

// convert UnicodeScalar to String
let myString = String(myUnicodeScalar)

// results
print(myString) // a

Lub alternatywnie:

let myInteger: Int = 97
if let myUnicodeScalar = UnicodeScalar(myInteger) {
    let myString = String(myUnicodeScalar)
}
 2
Author: Suragch,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-01-02 05:32:20
let a =123456888
var str = String(a)

Lub

var str = a as! String
 1
Author: Rob-4608,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-28 12:48:34

exampleLabel.text = String(yourInt)

 0
Author: Michael,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-05-10 05:12:39

Aby przekonwertować Łańcuch do Int

var numberA = Int("10")

Print(numberA) // It will print 10

Aby ukryć Int w String

var numberA = 10

1st way)

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

2nd way)

var strSomeNumber = String(numberA)

Lub

var strSomeNumber = "\(numberA)"
 0
Author: Anil Kukadeja,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-01-06 13:08:11

W swift 3.0 możesz zmienić liczbę całkowitą NA string, jak podano poniżej

let a:String = String(stringInterpolationSegment: 15)

Innym sposobem jest

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

/ / lub dowolna liczba całkowita zamiast 15

 0
Author: Dilip Jangid,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-06-02 13:27:06
let Str = "12"
let num: Int = 0
num = Int (str)
 0
Author: Paulo Silva,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-01-02 17:17:10