Search code examples
swift3uipickerview

extract integer by digit position Swift


I have several pickerViews throughout the app where users input data. Later, I will display this data as a "summary" page. Displaying the data is clearly easy. However, if the user wants to "edit" the data, I want the same digits to load in the pickerView. There will be case of 4 digits, 3 digits, 2 digits and a single digit. So, I need to have the ability to determine which digit is in which position, if there is a digit.

So, if the integer is 321, how to get "1" as the first digit, "2" as the second digit and "3" as the third digit.

Thank you


Solution

  • myPicker.selectRow(row, inComponent: 0, animated: true)
    

    You replace your row and 0 with your values. Then to get the numbers, only thing on my mind now would be something like

    while number > 0 {
        let digit = number%10 // do what you want with the digit
        number = round(number/10)
    }