Skip to content

Instantly share code, notes, and snippets.

@SlaunchaMan
Created June 17, 2015 18:38
Show Gist options
  • Save SlaunchaMan/369cbab0cc0df8bba321 to your computer and use it in GitHub Desktop.
Save SlaunchaMan/369cbab0cc0df8bba321 to your computer and use it in GitHub Desktop.
Getting the Next Element in a Swift Array
extension Array {
func after(item: T) -> T? {
if let index = find(self, item) where index + 1 < count {
return self[index + 1]
}
return nil
}
}
@SlaunchaMan
Copy link
Author

Compiling on Xcode 6.3.2:

Cannot invoke 'find' with an argument list of type '(Array<T>, T)'

@mfessenden
Copy link

This works for me with Xcode 7/Swift 2.2:

    extension Array where Element: Hashable {

        func after(item: Element) -> Element? {
            if let index = self.indexOf(item) where index + 1 < self.count {
                return self[index + 1]
            }
            return nil
        }
    }

@000Dmyers000
Copy link

I would like to control the array in sequence for back and next buttons, right now it selects the items randomly "Int(arc4random_uniform)" how can I change this? (I have not added the else/if functions yet)

@IBOutlet weak var slide: UIImageView!  

@IBOutlet weak var playNextButton: UIButton!
var slideArray:[String] = ["slide1", "slide2", "slide3", "slide4"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

@IBAction func nextButton(sender: UIButton) { 
    let slide = Int(arc4random_uniform(4))  
    let slideString:String = self.slideArray [slide]
    self.slide.image = UIImage(named: slideString)

}

}

@eastari
Copy link

eastari commented Jul 4, 2017

extension Array where Element: Hashable {

func next(item: Element) -> Element? {
    if let index = self.index(of: item), index + 1 <= self.count {
        return index + 1 == self.count ? self[0] : self[index + 1]
    }
    return nil
}

func prev(item: Element) -> Element? {
    if let index = self.index(of: item), index >= 0 {
        return index == 0 ? self.last : self[index - 1]
    }
    return nil
}

}

@eastari
Copy link

eastari commented Jul 4, 2017

previous added

@d4rkd3v1l
Copy link

@eastari Nice extension! But it should be enough to require the elements to be Equatable, isn't it? (instead of Hashable)

@ppeelen
Copy link

ppeelen commented Aug 19, 2021

@eastari Nice extension! But it should be enough to require the elements to be Equatable, isn't it? (instead of Hashable)

Correct. Here is a version for Swift 5+

extension Array where Element: Equatable {
    func nextItem(after: Element) -> Element? {
        if let index = self.firstIndex(of: after), index + 1 < self.count {
            return self[index + 1]
        }
        return nil
    }
}

@mvarie
Copy link

mvarie commented Aug 25, 2021

Another version, to get the preceding or following element of an Array (Collection), if any.

This version makes no assumptions about the nature of the index (it is not assumed that the index is an Int, nor it is assumed that the index is contiguous). Rather, index(after: index) and index(before: index) are used to determine the preceding/following index.

For forward lookups, the extension is applied to Collection, while for backwards lookups, it is applied to BidirectionalCollection: so that these functions can be used on any collection that supports the construct, not just the Array type.

These functions are not circular (asking for the element that follows the last element in an Array will return nil, not the first element).

As with any other solution offered on this page, it is assumed that the array has no duplicates. E.g., if your array is [A, B, C, B, E] and you're asking for the element that follows B, you will always get C.


extension Collection where Element: Equatable {
    
    func element(after element: Element) -> Element? {
        if let index = self.firstIndex(of: element){
            let followingIndex = self.index(after: index)
            if followingIndex < self.endIndex{
                return self[followingIndex]
            }
        }
        return nil
    }
}

extension BidirectionalCollection where Element: Equatable {
    
    func element(before element: Element) -> Element? {
        if let index = self.firstIndex(of: element){
            let precedingIndex = self.index(before: index)
            if precedingIndex >= self.startIndex{
                return self[precedingIndex]
            }
        }
        return nil
    }
}


@nrbrook
Copy link

nrbrook commented Sep 2, 2021

A slightly modified version of @mvarie's solution which optionally allows wrapping back to the start or end of the collection:

extension Collection where Element: Equatable {
    
    func element(after element: Element, wrapping: Bool = false) -> Element? {
        if let index = self.firstIndex(of: element){
            let followingIndex = self.index(after: index)
            if followingIndex < self.endIndex {
                return self[followingIndex]
            } else if wrapping {
                return self[self.startIndex]
            }
        }
        return nil
    }
}

extension BidirectionalCollection where Element: Equatable {
    
    func element(before element: Element, wrapping: Bool = false) -> Element? {
        if let index = self.firstIndex(of: element){
            let precedingIndex = self.index(before: index)
            if precedingIndex >= self.startIndex {
                return self[precedingIndex]
            } else if wrapping {
                return self[self.index(before: self.endIndex)]
            }
        }
        return nil
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment