如何在 Swift 5.5 中创建一个包含 Character in count String 的数组
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"]
此代码在 swift 5.5 中不起作用,我怎样才能以其他方式重复它?
解决方案:
var myStr = "Swift"
let myCh: Character = "S"
var myArr = [String]()
myStr.forEach {_ in
myArr.append("\(myCh)")
}
print(myArr)
var myStr = "Swift"
let myCh: Character = "S"
var myArr = Array(repeating: String(myCh), count: myStr.count)
myArr // ["S", "S", "S", "S", "S"]
此代码在 swift 5.5 中不起作用,我怎样才能以其他方式重复它?
解决方案:
var myStr = "Swift"
let myCh: Character = "S"
var myArr = [String]()
myStr.forEach {_ in
myArr.append("\(myCh)")
}
print(myArr)