Function should be called until my array become empty

I'm having an array of data, i need to call a API Function and use my array value. Each time the API is hitting respected value should be deleted and my function should be called until the array becomes empty.

1 answer

  • answered 2018-01-16 12:43 tailor

    Try this example. call this method process first time and pass your array like

    process(array)
    

    it will call itself until array contains element/s.

    let array = [1,2,3]
    
    func process(_ array: [Int]){
        if array.isEmpty{ return }
    
        let element = array.first!
        //process element
        print(element)
        let newArray = Array(array.dropFirst())
        process(newArray)
    }