Давайте предположим, что у нас есть текст вроде:
let text = """
This returns an array of strings. I am needing to be able to have the info for the ranges and also an ‘id’ for each one to be able to identify it. I have no idea how I would go about doing this. Do I need to make use of tokens somehow?
EDIT: What I mean by 'id' is just an identifier I can have to be able to recognise what paragraph relates to which component. This is because counting through the array would not be accurate if a paragraph is added between 2 already existing paragraphs.
"""
Получение components
из text
, разделенных "\n"
,
let arr = text.components(separatedBy: "\n")
Теперь нам нужно 3 вещидля каждого element
в arr
- String, Identifier, Range
var requiredArray = [(String, Range<String.Index>, Int)]()
for (index, str) in arr.enumerated() {
if let range = text.range(of: str) {
requiredArray.append(str, range, index)
}
}
print(requiredArray)
В приведенном выше коде
- мы использовали
index
из str
в arr
как его identifier
. - Рассчитано
range
из str
в text
Следовательно, requiredArray
содержит все paragraphs
в text
вместе с их identifier
и range
.
Дайте мне знать, если у вас все еще есть проблемы.