azurechen/ACTabScrollView

function currentPageIndex possible crash

Opened this issue · 2 comments

The function can throw an error if the width is equal to 0 - which may be possible in some cases.

So I replaced this:

private func currentPageIndex() -> Int {
        let width = self.frame.width
        var currentPageIndex = Int((contentSectionScrollView.contentOffset.x + (0.5 * width)) / width)
        if (currentPageIndex < 0) {
            currentPageIndex = 0
        } else if (currentPageIndex >= self.numberOfPages) {
            currentPageIndex = self.numberOfPages - 1
        }
        return currentPageIndex
    }

With this:

private func currentPageIndex() -> Int {
        let width = self.frame.width


        var currentPageIndex = 0
        if width != 0 {
           currentPageIndex =   Int(( contentSectionScrollView.contentOffset.x + (0.5 * width)) / width)
        } 


        if (currentPageIndex < 0) {
            currentPageIndex = 0
        } else if (currentPageIndex >= self.numberOfPages) {
            currentPageIndex = self.numberOfPages - 1
        }
        return currentPageIndex
    }

A simple validation to avoid division by zero.

Thank you! I will fix it at next version.
Or you can send a PR of your patch, I will very appreciate

is it fixed?