How to convert a String (numeric) in a Int array in Swift

后端 未结 8 1433
鱼传尺愫
鱼传尺愫 2021-02-13 20:41

I\'d like to know how can I convert a String in an Int array in Swift. In Java I\'ve always done it like this:

String myString = \"123456789\";
int[] myArray = n         


        
相关标签:
8条回答
  • 2021-02-13 20:49

    Swift 3: Functional Approach

    1. Split the String into separate String instances using: components(separatedBy separator: String) -> [String]

    Reference: Returns an array containing substrings from the String that have been divided by a given separator.

    1. Use the flatMap Array method to bypass the nil coalescing while converting to Int

    Reference: Returns an array containing the non-nil results of calling the given transformation with each element of this sequence.

    Implementation

    let string = "123456789"
    let intArray = string.components(separatedBy: "").flatMap { Int($0) }
    
    0 讨论(0)
  • 2021-02-13 20:51

    Swift 3

    Int array to String

    let arjun = [1,32,45,5]
        print(self.get_numbers(array: arjun))
    
     func get_numbers(array:[Int]) -> String {
            let stringArray = array.flatMap { String(describing: $0) }
            return stringArray.joined(separator: ",")
    

    String to Int Array

    let arjun = "1,32,45,5"
        print(self.get_numbers(stringtext: arjun))
    
        func get_numbers(stringtext:String) -> [Int] {
        let StringRecordedArr = stringtext.components(separatedBy: ",")
        return StringRecordedArr.map { Int($0)!}   
    }
    
    0 讨论(0)
  • 2021-02-13 20:59

    You can use flatMap to convert the characters into a string and coerce the character strings into an integer:

    Swift 2 or 3

    let string = "123456789"
    let digits = string.characters.flatMap{Int(String($0))}
    print(digits)   // [1, 2, 3, 4, 5, 6, 7, 8, 9]"
    

    Swift 4

    let string = "123456789"
    let digits = string.flatMap{Int(String($0))}
    print(digits)   // [1, 2, 3, 4, 5, 6, 7, 8, 9]"
    

    Swift 4.1

    let digits = string.compactMap{Int(String($0))}
    

    Swift 5 or later

    We can use the new Character Property wholeNumberValue https://developer.apple.com/documentation/swift/character/3127025-wholenumbervalue

    let digits = string.compactMap{$0.wholeNumberValue}
    
    0 讨论(0)
  • 2021-02-13 21:04
    let array = "0123456789".compactMap{ Int(String($0)) }
    print(array)
    
    0 讨论(0)
  • 2021-02-13 21:05

    Swift 3 update:

    @appzYourLife : That's correct toInt() method is no longer available for String in Swift 3. As an alternative what you can do is :

    intArray.append(Int(String(chr)) ?? 0)
    

    Enclosing it within Int() converts it to Int.

    0 讨论(0)
  • 2021-02-13 21:06
    var myString = "123456789"
    var myArray:[Int] = []
    
    for index in 0..<countElements(myString) {
        var myChar = myString[advance(myString.startIndex, index)]
        myArray.append(String(myChar).toInt()!)
    }
    
    println(myArray)   // [1, 2, 3, 4, 5, 6, 7, 8, 9]"
    

    To get the iterator pointing to a char from the string you can use advance

    The method to convert string to int in Swift is toInt()

    0 讨论(0)
提交回复
热议问题