Hi Guys,
Having decided to start looking at swift I decided to translate an old unfinished project from Objective C. I have some code translated (that is working nicely) but uses integers to iterate over a string:
let text = "this is a piece \n of text \n over multiple lines."
let cursorPosition = 17
var startIndex = 0
if (cursorPosition > 0){
for counter in stride(from: cursorPosition, through:0, by: -1){
startIndex = counter
var index = text.index(text.startIndex, offsetBy:counter)
if (text[index] == "\n") {
break
}
}
}
However, I have read that this is really inefficient as each time the index is found (line 10) swift iterates through the whole string.
Do you any advice on how I could improve the efficiency of the code ?
Best Regards
Gethyn