0

I have a picker view of int in my Swift code. It has numbers 1-7 in an array called numbers. When I ask to print numbers[row] inside the didSelectRow function it prints the correct numbers. But within my submit tapped function it only prints 0. It did this in viewWillAppear as well.

I essentially want the row to be a variable that I can use as an int to delay some code.

let numbers = [1,2,3,4,5,6,7]
var days = Int()

override func viewDidLoad() {
    super.viewDidLoad()

    self.pickerView.delegate = self
    self.pickerView.dataSource = self
}

override func viewWillAppear(_ animated: Bool) {
    print(days)
}

func numberOfComponents(in pickerView: UIPickerView) -> Int {
    return 1
}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    return String(numbers[row])
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return numbers.count
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    var days = numbers[row]
    print(days)
}

@IBAction func submitTapped(_ sender: Any) {
    databaseRef.child("numbers").child(self.TextField.text!).setValue(["thename" : UserDefaults.standard.value(forKey: "nametext")])
    print(days)
    DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(days) , execute: {
        databaseRef.child("numbers/\(self.TextField.text!)").removeValue()
    })

Does anyone know why it is always returning zero in the submit tapped function?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Michael R.
  • 25
  • 9

2 Answers2

1

Simply replace

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    var days = numbers[row]
    print(days)

with

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    days = numbers[row]
    print(days)

Your code doesn't work because you are creating new variable and you are not referencing your instance variable.

kiwisip
  • 437
  • 1
  • 4
  • 12
0

You're initializing days using the Int() constructor, which is always 0. Thus, in your viewWillAppear(_:) method later, 0 is printed out, because that's what days was initialized to be. However, in your pickerView(_:didSelectRow:inComponent:) method, you're using the var keyword to create a new variable called days that is local to the pickerView(_:didSelectRow:inComponent:) method, and the global days variable is never changed, and is always equal to 0. Therefore, later on in your submitTapped(_:) method, the global days is still 0.

I'm going to assume you want to set the global days variable in your pickerView(_:didSelectRow:inComponent:) method to numbers[row]. To do so, you must remove the var keyword next to days inside the pickerView(_:didSelectRow:inComponent:). Inside that method, you can reference it either as days or self.days, as long as you don't use the var or let keyword to redeclare days inside that method

Michael Hulet
  • 3,253
  • 1
  • 16
  • 33