Q:

UICollectionView.setContentOffset(...) causing NSRangeException

I've got a UICollectionView representing a calendar, with 13 months of data generated on load and a section for each month.  There's a button in the UI to toggle between the current year and the next year, but when scrolling from the end of the collection back to the start of the current year the app crashes with the following error. 
'NSRangeException', reason: '*** -[__NSArrayM objectAtIndexedSubscript:]: index 9223372036854775806 beyond bounds [0 .. 4]'
This is the method causing the error. 
public func scrollTo(year: Int) {
    if let month = months.firstIndex(where: { $0.components.year == year }),
        let attributes = collectionView.layoutAttributesForSupplementaryElement(ofKind: UICollectionElementKindSectionHeader, at: IndexPath(item: 0, section: month)){
        let y = attributes.frame.origin.y - collectionView.contentInset.top

        // This is the line causing the error
        collectionView.setContentOffset(CGPoint(x: 0, y: y), animated: true)
    }
}

I've tried printing out my calculated y value, but it's been 0 every time it crashes. 
It might be worth noting that the index mentioned in the exception is equal to Int.max - 1, although seeing as y is a CGFloat I'm not sure how this is relevant. 

A:

I'm not sure that the animation was causing the crash, but changing animated to false in the call to setContentOffset(...) fixed the problem.