Casting floats and UILabel - iphone

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

NSString has a convenient floatValue method:
rate = [hourlyRate.text floatValue];

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;

Related

Syntax for Extracting CGFloat out of NSDictionary

Just wondering what the syntax would be to extract a CGFloat out of an NSDictionary like follows:
slider.minimumValue = [filterAttributes valueForKey:kCIAttributeSliderMin];
An NSDictionary only holds objects. What kind of object would wrap a primitive like CGFloat? NSNumber would make sense. Now, since CGFloat is either a float or a double, you'll probably want to get the double value to preserve precision/range.
Therefore:
slider.minimumValue = [[filterAttributes valueForKey:kCIAttributeSliderMin] doubleValue];
You can only put OBJECTS into an NSDictionary (or NSARRAY). CGFloat is a literal (just maps to a float), so you can't put it into or retrieve it from the dictionary.
Instead, wrap it as an NSNumber (when you add it to the dictionary), which is an object.
NSNumber *sliderMin = [NSNumber numberWithFloat:kCIAttributeSliderMin]
Or using the new syntax, you can just say #kCIAttributeSliderMin or #(kCIAttributeSliderMin) to autobox as an NSNumber.
To get the value back out, you'll retrieve the object as an NSNumber then say, [myNumber floatVal] to get the NSFloat.
Finally, you probably want to say "objectForKey" not "valueForKey".
update - sorry, in your example you're treating kCIAttributeSliderMin as a key, and I'm using it as the 'value'; but I think you get the point. Store an NSNumber object; retrieve an NSNumber object. Sorry for any confusion swapping that may have caused.

CGFloat from a float?

How do you cast or create a CGFloat from a float?
I get a bad receiver type float * error.
CGFloat point = [graphValues objectAtIndex:0];
graphValues is probably not an NSArray, but a C-array of floats. If so, you want this:
CGFloat point = graphValues[0];
(The reason I think this is because it's telling you you're trying to call an Objective-C method on a float *.)
That's because the array is not an NSArray but a float array.
CGFloat point = graphValues[0];
If you want to know how CGFloat is declared, have a look in the header (it should be a double).

How do I Getting a TextField to do simple math?

I have three UITextFields. Two of them represent a certain number value. The third represents the percentage of the two. How do I setup the 3rd UITextField to do this simple math?
You can simply get the intValue or the floatValue or the doubleValue of the text that you have received from the first two text fields. Eg:
float firstFloat = [self.firstTextField.text floatValue];
float secondFloat = [self.secondTextField.text floatValue];
float answer = firstFloat / secondFloat; //or whatever math you need to do
self.thirdTextField.text = [NSString stringWithFormat:#"%.2f",answer];
Hope this helps.
Check out "Getting Numeric Values" here:
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html
So you can use that to convert from the text property of your two UITextField instances.
Then you can convert them back to an NSString and plug them into the text of the third UITextField by using initWithFormat (something like [initWithFormat:#"%d", theResult]).
You can also do it in the following way.
NSString *str1,*str2;
str1=text1.text;
str2=text2.text;
int num1=[str1 intValue];
int num2 =[str2 intValue];
int ans=num+num2;
text3.text=[NSString stringWithFormat:#"%d",ans];
Hope this helps.
[answer setStringValue:[NSString stringWithFormat:#"%2.02g", answerFloat]];

Accessing methods from another class

I am new to the C objective and I'm having lots of difficulties. Hope you goys would be able to help me out.
Alright, I have a view controller class that displays the data from the external sensor plugged to an iphone. I have another database class that is supposed to grab that data and store it in an array which can be used to plot a graph.
I'm having difficulties in finding a way to capture the data captured by the view controller class method variables and using it to store in database class.
The code below is from View Controller class which captures analog signal and displays in UILabel.
(void) forceCalculationKg{
NSNumber *number = [controller. analogInValues objectAtIndex:0];
[controller enableDigitalInputs:YES];
double value = [number doubleValue];
double force;
force = 0.2908 *pow(2.718,(1.2089 * value));
double forcekg;
forcekg = force/2.2;
forceoutput.text = [NSString stringWithFormat:#" %0.1f", forcekg];
}
You didn't submit code for this question that actually compiles. But in any case, I assume your forceCalculationKg method is the function on your controller that you are calling to retrieve the sensor data? You are then applying a calculation to the data and displaying it, right?. To keep this data around, just add a NSMutableArray property to your controller and save each transformed data point to it.
-(void) forceCalculationKg {
NSNumber *number = [controller.analogInValues objectAtIndex:0];
[controller enableDigitalInputs:YES];
double value = [number doubleValue];
double force = 0.2908 *pow(2.718,(1.2089 * value));
double forcekg = force/2.2;
[self.datapoints addObject: [NSNumber numberWithDouble: forcekg]];
forceoutput.text = [NSString stringWithFormat:#" %0.1f", forcekg];
}
Where datapoints is a NSMutableArray property you add to your view controller via the #property and #synthesize keywords. You can then pass the datapoints array around your program to wherever you need it.

How do you pass a float to ProgressView from another class?

I'm trying to pass ProgressView a float from a calculation made in another class. I've tried passing it by converting it to a NSDecimalNumber but I can't get it back to a float again when it reaches the destination. There's got to be a better way than this.
myUIProgressView.value = someFloat; // where someFloat is just type float between 0.0..1.0
Should work. If you're using an NSNumber to hold the value, you can use [myNSNumber floatValue]; to get its float representation.