A
A
Aleksandr Govorukhin2017-11-09 22:44:04
Swift
Aleksandr Govorukhin, 2017-11-09 22:44:04

How to start a for loop from a given index in Swift 4?

Hello, the question itself is in the title:

if let range = getHtml.range(of:"recipeInstructions") {
            let startPos = getHtml.distance(from: getHtml.startIndex, to: range.lowerBound)
            let endPos = getHtml.distance(from: getHtml.startIndex, to: range.upperBound)
            print(startPos, endPos)

            //Как начать с endPos?)
            for element in getHtml {

            }
        }

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Maxim Globak, 2017-11-10
@SnapSh0t

You can use the good old cyclefor in

// Твой массив
let array = [0,1,2,3,4,5,6,7,8,9,9,8,7,6,5,4,3]
// Позиция, с которой стоит начать прогон по массиву
let endPos = 5
// Так как начало задаем сами,
// то надо быть уверененными,
// что endPos входит в рамки массива
if endPos < array.count {
  // старый добрый For in
  for i in endPos..<array.count {
    // Получаем элемент из массива по индексу
    // Это очень быстрая операция, не стоит переживать из-за времени
    let item = array[i]
    // дальше совершаем какие-то действия с элементом массива
    print(item)
  }
}

A
Alexander Semchenko, 2017-11-10
@0xcffaedfe

someArray.reversed()

B
briahas, 2017-12-01
@briahas

Create an array containing the elements starting with the element at position endPos and then forin

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question