You can do something like this.
First you need to set up a position counter that tells you where you are right now. Important: you need to set the start to 0 if you want to start from the first element, because arrays start counting from 0:
var swipePosition = 0
Then, if you swipe forward, you need to check if the current swipePosition is greater than the number of images. For this you can use the count method of your array. But you have to subtract 1, because the position starts at 0. Therefore, if it is already at the highest or lowest position in your array, you do not need to do anything. Otherwise, you add or subtract one position:
//swipe forward if swipePosition > imageList.count-1{ //do nothing because it is already at the highest position }else{ swipePosition += 1 } image.image = UIImage(named: imageList[swipePosition]) //swipe backward if swipePosition == 0 { //Do nothing because it is already at start. }else{ swipePosition -= 1 } image.image = UIImage(named: imageList[swipePosition])
source share