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.
Related
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.
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).
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]];
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;
In my app, I am saving coordinates from an MKMapView into a property list. After the user hits "save" I set the center coordinate of the selection view to that on the main view, and then save the mapView.centerCoodinate.latitude and longitude into a pList. However, this gives me a value like "1078114215" which the map says is not a vail coordinate. What am I doing wrong?
Saving a pointer instead of the two floats in the coordinate? Not saving as a float?
Sounds like you're accidentally mis-typing your double variable. When you add it to your dictionary to be stored as a plist be sure to transform it from a double to an NSNumber like this:
[myDictionary addObject:[NSNumber numberWithDouble:latitude] forKey:#"latitude"];
and when you retrieve it, transform it from an NSNumber to a double:
double latitude = [[myDictionary objectForKey:#"latitude"] doubleValue];