muiraie
muiraie

Reputation: 59

Fatal error: Float value cannot be converted to Int because it is either infinite or NaN

I'm getting a fatal error trying to convert a float to an Int.

Debugging:

Okay so have this line to keep track of page scroll.

Float(scrollView.contentOffset.x) / Float(scrollView.frame.size.width)

This gives back values such as:

2.031401 2.0241547 2.018116 2.013285 2.009662 2.0072465 2.0048308 2.0036232 2.0024154 2.0012078 2.0

we want to round it down; we don't want to have a floating point as a decimal point which can be a result of a sloppy halfway scroll

Here I'm using So I using floorf: floorf(Float(scrollView.contentOffset.x) / Float(scrollView.frame.size.width))

Which gives back values like: 0.0, 1.0, 2.0. perfect. But the var currentPage only takes Int

now we convert it to Int: pageControl.currentPage = Int(floorf(Float(scrollView.contentOffset.x) / Float(scrollView.frame.size.width)))

which produces the following error:

Float value cannot be converted to Int because it is either infinite or NaN

Full code:

import UIKit

class pageViewController: UIViewController {

private let scrollView = UIScrollView()

private let pageControl: UIPageControl = {
    let pageControl = UIPageControl()
    pageControl.numberOfPages = 5
    pageControl.backgroundColor = .systemBlue
    return pageControl
}()

override func viewDidLoad() {
    super.viewDidLoad()
    scrollView.delegate = self
    scrollView.backgroundColor = .red
    pageControl.addTarget(self,
                          action: #selector(pageControlDidChange(_:)),
                          for: .valueChanged)
    view.addSubview(scrollView)
    view.addSubview(pageControl)
}

@objc private func pageControlDidChange(_ sender: UIPageControl) {
    let current = sender.currentPage
    scrollView.setContentOffset(CGPoint(x: CGFloat(current) * view.frame.size.width,
                                        y: 0), animated: true)
}

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    pageControl.frame = CGRect(x: 10,
                               y: view.frame.size.height-100,
                               width: view.frame.size.width-20,
                               height: 70)
    
    scrollView.frame = CGRect(x: 0,
                              y: 0,
                              width: view.frame.size.width,
                              height: view.frame.size.height-100)

    if scrollView.subviews.count == 2 {
        //2 is because a view by default does not have any subviews. whereas a scroll view does have a subview by default. A scroll view inherently starts with 2 subviews; vertical and horizontal scroll indicators
        configureScrollViews()
    }
}

private func configureScrollViews() {
    scrollView.contentSize = CGSize(width: view.frame.size.width*5, height: view.frame.size.height)
    scrollView.isPagingEnabled = true //snaps pages to the middle of the screen
    
}
}

The issue is in the func below

extension pageViewController: UIScrollViewDelegate {

///everytime a scrollView scrolls we want to calculate what page we're on. This sums up this func.
func scrollViewDidScroll(_ scrollView: UIScrollView) {
    pageControl.currentPage = Int(floorf(Float(scrollView.contentOffset.x) / Float(scrollView.frame.size.width)))   //we want to round it down; we dont want to have a floating point as a decimal point which can be a result of a a sloppy halfway scroll
    let colors: [UIColor] = [
        .systemRed,
        .systemPink,
        .systemTeal,
        .systemGray,
        .systemIndigo
    ]
    for x in 0..<5 {
        let page = UIView(frame: CGRect(x: CGFloat(x)*view.frame.size.width, y: 0, width: view.frame.size.width, height: scrollView.frame.size.height))
        page.backgroundColor = colors[x]
        scrollView.addSubview(page)
    }
    
    //print(floorf(Float(scrollView.contentOffset.x) / Float(scrollView.frame.size.width))   )
}
}

Upvotes: 0

Views: 1216

Answers (2)

Taylan Atasoy
Taylan Atasoy

Reputation: 1

i have resolved this issue with this code:

let pageNumber = round(scrollView.contentOffset.x / view.frame.width)

pageControl.currentPage = Int(pageNumber)

Upvotes: 0

Saranjith
Saranjith

Reputation: 11577

You can debug in the method below and print the value of:

floorf(Float(scrollView.contentOffset.x) / Float(scrollView.frame.size.width))

And understand the value ranges; check whether any NaN or Infinite values are getting printed.

Upvotes: 0

Related Questions