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

后端 未结 8 1422
鱼传尺愫
鱼传尺愫 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: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)!}   
    }
    

提交回复
热议问题