Я настраиваю хорошее решение для того, чтобы прочитать комментарии в 3 шага
extension String {
func addSeperator(_ seperator:String ,after nWords : Int) -> String{
// 1 • create array of words
let wordsArray = self.components(separatedBy: " ")
// 2 • create squence for nwords using stride
// Returns a sequence from a starting value to, but not including, an end value, stepping by the specified amount.
let sequence = stride(from: 0, to: wordsArray.count, by: nWords)
//3 • now creat array of array [["",""],["",""]...etc] and join each sub array by space " "
// then join final array by your seperator and add space
let splitValue = sequence.map({Array(wordsArray[$0..<min($0+nWords,wordsArray.count)]).joined(separator: " ")}).joined(separator: " \(seperator) ")
return splitValue
}
}
print(paragraph.addSeperator("$$$", after: 2))