Home>

This is a screen shot using the standardUICollectionViewin an iOS app (Swift).
The OS is verified with iOS 11.4 and 12.1.
In addition, although it is confirmed only on the simulator at hand, it has already occurred in the actual machine because a crash under the exact same conditions has already been reported in production.

Is implemented with JSQMessagesViewController for screens for exchanging messages.

Some parts of the screen are scrolled to the specified position with thescrollToItemmethod.

self.collectionView! .scrollToItem (
    at: IndexPath (item: itemIndex, section: 0),
    at: UICollectionView.ScrollPosition.top,
    animated: false
)
Error message

Of the first argument,at: IndexPath (item: itemIndex, section: 0),is minus toitemon iOS 11 devices and iOS 12 or later The behavior when the value of is specified is different.
In other words, if you execute the following code

self.collectionView! .scrollToItem (
    at: IndexPath (item: -1, section: 0), // specify item as a negative value
    at: UICollectionView.ScrollPosition.top,
    animated: false
)
  • Is n’t really falling on iOS12
  • Crash when trying on iOS 11.4

It's natural, but even iOS 11.4 didn't crash unless a value less than 0 was specified.

self.collectionView! .scrollToItem (
    at: IndexPath (item: max (-1, 0), section: 0), // max should not be less than 0
    at: UICollectionView.ScrollPosition.top,
    animated: false
)

If i specify antoo large index (an index greater thanself.collectionView.count), it will fail withNSInvalidArgumentExceptionregardless of the OS version.

self.collectionView! .scrollToItem (
    at: IndexPath (item: 100, section: 0), // with self.collectionView.count = 3
    at: UICollectionView.ScrollPosition.top,
    animated: false
)
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'attempt to scroll to invalid index path:{length = 2, path = 0-100}'

I know how to deal with it for the time being, but I didn't have any changes to the language specification or other reports, so I thought it was unpleasant. I would like to ask if there is any.

  • Answer # 1

    Thank you for your reply.
    As you say, it is strange that you are passing a value that is not possible in the first place, and it is unavoidable to discuss whether there is a correct behavior.