0

My UIPickerView with four components gives me this back when I'm trying to build it. What should I do I mean I made the number Of Rows one Point bigger because in Swift numbers start at 0 but nothing worked.... What is the problem?

let componentOne = [1...1000]
    let componentTwo = [1...59]
    let componentThree = [1...59]
    let componentFour = [1...10]
    
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 4
    }
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        if component == 0 {
            return 1000
        }
        if component == 1 {
            return 59
        }
        if component == 2 {
            return 59
        }
        if component == 3 {
            return 10
        }
        return component
    }
        
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        if component == 0 {
            _ = componentOne[row]
            return "\(1000)"
            }
            if component == 1 {
                _ = componentTwo[row]
                return "\(59)"
                }
                if component == 2 {
                    _ = componentThree[row]
                    return "\(59)"
                    }
                    if component == 3 {
                        _ = componentFour[row]
                        return "\(10)"
                        }
        return nil
                    }
    
7
  • 1
    What are you trying to achieve? Is componentOne an Array? Commented Nov 11, 2020 at 4:36
  • 1
    Which line of code do you get the error? Commented Nov 11, 2020 at 4:37
  • @aheze in this line: if component == 0 { Commented Nov 11, 2020 at 5:06
  • @Rob yes of course Commented Nov 11, 2020 at 5:07
  • Consider that [1...10] is an array of one range, not an array of 10 integers. Commented Nov 11, 2020 at 5:27

1 Answer 1

1

Consider that [1...10] is an array of one range, not an array of 10 integers.

You have to write

let componentOne = Array(1...1000)
let componentTwo = Array(1...59)
let componentThree = Array(1...59)
let componentFour = Array(1...10)

To avoid those errors don't hard-code the return values of numberOfRowsInComponent.

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    switch component {
        case 0: return componentOne.count
        case 1: return componentTwo.count
        case 2: return componentThree.count
        case 3: return componentFour.count
        default: return 0
    }

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.