Search code examples
arraysswiftstringcountcharacter

How to create an array with Character in count String in Swift 5.5


var myStr = "Swift"

let myCh: Character = "S"

var myArr = Array(repeating: String(myCh), count: myStr.count)
myArr        //   ["S", "S", "S", "S", "S"]

This code doesn't work in swift 5.5, how can i repeat it in other way?


Solution

  • Solution:

    var myStr = "Swift"
    let myCh: Character = "S"
    var myArr = [String]()
    
    myStr.forEach {_ in
        myArr.append("\(myCh)")
    }
    
    print(myArr)