Kyle Luchinski
Kyle Luchinski

Reputation: 153

Casting floats and UILabel

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];

Upvotes: 0

Views: 660

Answers (2)

Espresso
Espresso

Reputation: 4752

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;

Upvotes: 1

user467105
user467105

Reputation:

NSString has a convenient floatValue method:

rate = [hourlyRate.text floatValue];

Upvotes: 1

Related Questions