CGFloat from a float? - iphone

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).

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.

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

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

How do I add a CGPoint to NSMutableArray?

I want to store my CGPoint to the NSMutable Array, so , I have method like this:
[self.points addObject:CGPointMake(x, y)];
But I got the error, it said that :
Incompatible type for argument 1 of
"addObject".
So, I check out the API,
- (void)addObject:(id)anObject
anObject The object to add to the end
of the receiver's content. This value
must not be nil.
So, I think the "CGPointMake" can make a Object, but it can't be assigned. What happens?
The problem is that CGPoint is actually just a C structure it is not an object:
struct CGPoint {
CGFloat x;
CGFloat y;
};
typedef struct CGPoint CGPoint;
If you are on the iPhone you can use the NSValue UIKit additions to convert the CGPoint to an NSValue object.
See this previous answer for examples: How can I add CGPoint objects to an NSArray the easy way?
You can also do the following:
[myArray addObject:[NSValue valueWithCGPoint:MyCGPoint]];
Unfortunately for you a CGPoint isn't an Objective-c object. It is a c struct. if you Apple double click on CGPoint you should jump to the definition
struct CGPoint {
CGFloat x;
CGFloat y;
};
typedef struct CGPoint CGPoint;
If you want to store CGPoint in an NSArray you will need to wrap them first. You can use NSValue for this or write your own wrapper.
see Converting a CGPoint to NSValue
EDIT> There is a small overhead for each objective-c method call, and creating and destroying objects involves many method calls before they are even used for anything. You shouldn't worry about this normally but for very small objects which encapsulate little behaviour and that have short lifetimes it can affect performance. If Apple used objects for all points, rect, sizes and even ints, floats, etc performance would be worse.
To build on the answer given by atbreuer11, you can convert your CGPoint to NSValue, store it in NSMutableArray and convert it back using the following:
//Convert CGPoint and Store it
CGPoint pointToConvert = CGPointMake(100.0f, 100.0f);
NSValue *valueToStore = [NSValue valueWithCGPoint:pointToConvert];
NSMutableArray *arrayToKeep =[NSMutableArray arrayWithObject:valueToStore];
Then restore it again:
CGPoint takeMeBack;
for (NSValue *valuetoGetBack in arrayToKeep) {
takeMeBack = [valuetoGetBack CGPointValue];
//do something with the CGPoint
}
That's probably the easiest way to do it. You can write a complete class and do all types of data manipulation, but I think it would be an overkill, unless you really have to.
EDIT
For Swift 5 (I'm not sure why one would want to do this, given that we can use literal arrays nowadays, but here goes):
Save Values:
let somePoint = CGPoint(x: 200, y: 400)
let array = NSMutableArray(array: [somePoint])
To retrieve it:
let points = array.compactMap({ ($0 as? NSValue)?.cgPointValue })
Swift 3.x
// Convert CGPoint to NSValue
let cgPoint = CGPoint(x: 101.4, y: 101.0)
let nsValue = NSValue(cgPoint: cgPoint)
var array = NSArray(object: nsValue)
// Restore it again
var cgPoint : CGPoint!
for i in array {
cgPoint = i as? CGPoint
}
A simple way to handle CGPoint (or any other non NSObject inherited structure) is to create a new class inherited from NSObject.
The code is longer, but clean. An example is below:
In .h file:
#interface MyPoint:NSObject
{
CGPoint myPoint;
}
- (id) init;
- (id) Init:(CGPoint) point;
- (BOOL)isEqual:(id)anObject;
#end
In .m file:
#implementation MyPoint
- (id) init
{
self = [super init];
myPoint = CGPointZero;
return self;
}
- (id) Init:(CGPoint) point{
myPoint.x = point.x;
myPoint.y = point.y;
return self;
}
- (BOOL)isEqual:(id)anObject
{
MyPoint * point = (MyPoint*) anObject;
return CGPointEqualToPoint(myPoint, point->myPoint);
}
#end
Here is some code sample showing the usage, do not forget to release!!!
//init the array
NSMutableArray *pPoints;
pPoints = [[NSMutableArray alloc] init];
// init a point
MyPoint *Point1 = [[MyPoint alloc]Init:CGPointMake(1, 1)];
// add the point to the array
[pPoints addObject:[[MyPoint alloc] Point1]];
//add another point
[Point1 Init:CGPointMake(10, 10)];
[pPoints addObject:[[MyPoint alloc] Point1]];
[Point1 Init:CGPointMake(3, 3)];
if ([pPoints Point1] == NO))
NSLog(#"Point (3,3) is not in the array");
[Point1 Init:CGPointMake(1, 1)];
if ([pPoints Point1] == YES))
NSLog(#"Point (1,1) is in the array");

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.