Why is my Swift loop failing with error “Can't form range with end < start”?

后端 未结 6 1629
北海茫月
北海茫月 2021-02-05 01:44

I have a for loop that checks if a number is a factor of a number, then checks if that factor is prime, and then it adds it to an array. Depending on the original number, I wil

6条回答
  •  失恋的感觉
    2021-02-05 02:17

    Using a simple solution you can make for loop reverse using Stride and -

    
    
    func reverseArray(oldarray: [String]) -> [String] {
            var newArray = [String]()
            let len = oldarray.count
            for i in stride(from: len - 1, through: 0, by: -1)
            {    newArray.append(oldarray[i])
                print(i)
            }
            return newArray
        }
    
    input :  print(reverseArray(oldarray: ["World", "Hello"]))
    
    output : ["Hello", "World"]
    

提交回复
热议问题