Can someone tell me why the following code is wrong:
def pickerView(pickerView, didSelectRow:row, inComponent:component)
@myLabel.text = "#{row+1}"
end
def remain
@remainLabel.text = @myLabel.text
end
def left
remain.to_i (+1)
end
I'm calling the "left" method via a button click but I get "invalid radix 1" as an error. I have tried various different configurations including with and without the brackets and with and without spaces. Basically, whatever I do, I either get an error or else just the selected number from the picker without the 1 added.
You want:
remain.to_i + 1
The method remain
returns a string, the string in @myLabel.text
. When you write:
remain.to_i (+1)
Ruby thinks you are passing the parameter (+1)
to the to_i
method. It turns out that to_i
does take a parameter which is the base (or radix) you want it to use to interpret the string.
puts "FF".to_i(16) // prints "255"
puts "101".to_i(2) // prints "5"
Base 1 is an invalid base hence the invalid radix message. The default base is 10, so
puts "123".to_i(10) // prints "123"
puts "123".to_i // prints "123"