Casting floats and UILabel

667 Views Asked by At

HI -

I have a value in a UILabel and I want to pass the number to a value that is of float type.

rate = float
hourlyRate = label

Below is my code and im getting errors. I know its bad form to go from object to primitive values but there has to be a way.

Any help would be appreciated!

rate = NSNumber numberWithFloat:[hourlyRate.text rate];
2

There are 2 best solutions below

0
On BEST ANSWER

NSString has a convenient floatValue method:

rate = [hourlyRate.text floatValue];
0
On
rate = NSNumber numberWithFloat:[hourlyRate.text rate];

That’s invalid because if you’re trying to send the message numberWithFloat: to NSNumber it has to be enclosed in brackets, like so:

rate = [NSNumber numberWithFloat:[hourlyRate.text rate]];

And what’s more hourlyRate.text returns an NSString. You can’t send an NSString the method rate unless you subclassed it and added that method.

This is the right way to get the float value of UILabel, try this:

rate = [hourlyRate.text floatValue];

And do you mean:

float rate;