Im not sure how to get this to work: I was thinking something to do with typedef, but I can't find much on the subject.
float (^pixelsToDistance)(float, float, NSString *) = ^(float distance, float scale, NSString *conversion)
{
// Code goes here
}
Im trying to return a float value from this block function.
Should I use a function instead?
You were close. You want:
float (^pixelsToDistance)(float, float, NSString *) = ^float(float distance, float scale, NSString *conversion) { ... };
Note the return type after the ^ on the right hand side of the assignment operator.
As the commentator points out, you can omit the return type if it's clear to the compiler what you're returning from the block. E.g.:
float (^pixelsToDistance)(float, float, NSString *) = ^(float distance, float scale, NSString *conversion) { return 0.0f };
Related
I've been trying to pull the value "tempF" from /var/mobile/Library/Preferences/com.skylerk99.snappref.plist to return for the float value.
-(float) temperatureDegFahrenheit {
NSDictionary *pref = [[NSDictionary alloc]initWithContentsOfFile:#"/var/mobile/Library/Preferences/com.sky.snap.plist"];
NSString *tempF;
if( [[pref objectForKey:#"showF"] boolValue] ) {
return tempF;
}
else {
return %orig;
}
}
but I keep getting the error
cannot initialize return object of type 'float' with an lvalue of type 'NSString *'
return tempF;
What is the best way to accomplish this, because I obviously don't know?
Update:
Based on the discussion below, let's assume the temperature is entered through a UITextField called temperatureTextField. You can get the float value of the input temperature like this:
[[temperatureTextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] floatValue];
The function expects you to return float, but tempF is a NSString *.
To parse float value from a NSString, try this (by the way, I assume that you did not show all your code, otherwise your tempF was never assigned):
[tempF floatValue];
I have two NSInteger variables called "domande" and "corrette". I have to execute this operation with them: corrette*10/domande. I want the result to be a float variable, so I declared a "voto" variable as so: "float voto = corrette*10/domande;" . When I output the value of "voto" with NSLog I get an approximated value of the result followed by ".000000".
Here's the code:
NSInteger domande = [numDomande integerValue];
NSInteger corrette = [numRisposteCorrette integerValue];
float voto = corrette*10/domande;
NSLog(#"float value is: %f", voto);
When I assign to "domande" a value of 7, and to "corrette" a value of 4: voto=5.000000
Instead it should be voto=5.71...
How can I have the division return not an integer type converted to float, but directly a float type?
Simplest way is to do:
float voto = 10.0f * corrette / domande;
By making the first argument a float, you guarantee that the others will be promoted as well and that intermediate and final results will not suffer truncation.
You could achieve a similar result by casting corrette to a float but I tend to prefer simplicity where possible.
Rather than converting integers to floats, you could just get floats in the first place:
CGFloat domandeFloat = [numDomande floatValue];
CGFloat corretteFloat = [numRisposteCorrette floatValue];
CGFloat voto = (corretteFloat / domandeFloat) * 10.0f
NSLog(#"float value is: %f", voto);
NSInteger does not have a method called floatValue. NSInteger is just an int. Instead, the solution would be:
CGFloat domandeFloat = [[NSNumber numberWithInt: numDomande] floatValue];
CGFloat domandeFloat = [[NSNumber numberWithInt: numRisposteCorrette] floatValue];
CGFloat voto = (corretteFloat / domandeFloat) * 10.0f;
Try to convert the NSIntegers to a float type first:
float voto = (float)corrette*10/(float)domande;
you can cast "10" from int to float by writing it as "10.0"
float voto = corrette*10.0/domande;
or
float voto = ((float)corrette*10) / (float)domande;
Operation "/" is returning type of it operands - 5/4 will return int result 1 because 5 and 4 are int, and 5.0/4.0 will return 1.25, because 5.0 and 4.0 are interpreted as float values. So you should manually cast type of input variables corrette and domande to float
typedef struct {
float Position[3];
float Color[4];
float VertexNormal[3];
} Vertex;
typedef struct WingedEdge{
struct WingedEdge* sym;
struct WingedEdge* next;
struct WingedEdge* prev;
Vertex** vertex;
GLushort** indexPointer;
} WingedEdge;
Vertex* vertices;
GLushort* indices;
struct WingedEdge* wingedEdges;
int numberOfVertices; //initialized elsewhere
int numberOfIndices; //initialized elsewhere,this is multiplied by three since I am not using a struct for the indices
vertices = (Vertex *) malloc(numberOfVertices * sizeof(Vertex));
indices = (GLushort *) malloc(numberOfIndices * sizeof(GLushort) * 3);
wingedEdges = (struct WingedEdge*)malloc(sizeof(struct WingedEdge)*numberOfIndices*3);
for (int i = 0; i < numberOfIndices*3; i+=3) {
wingedEdges[i].indexPointer = (&indices+i);
wingedEdges[i+1].indexPointer = (&indices+i);
wingedEdges[i+2].indexPointer = (&indices+i);
wingedEdges[i].vertex = (&vertices+indices[i]);
wingedEdges[i+1].vertex = (&vertices+indices[i+1]);
wingedEdges[i+2].vertex = (&vertices+indices[i+2]);
NSLog(#"%hu %hu %hu", *(indices+i),*(indices+i+1),indices[i+2]);
NSLog(#"%f %f %f", (vertices+indices[i])->Position[0], (vertices+indices[i])->Position[1], (vertices+indices[i])->Position[2]);
NSLog(#"%f %f %f", (vertices+indices[i+1])->Position[0], (vertices+indices[i+1])->Position[1], (vertices+indices[i+1])->Position[2]);
NSLog(#"%f %f %f", (vertices+indices[i+2])->Position[0], (vertices+indices[i+2])->Position[1], (vertices+indices[i+2])->Position[2]);
NSLog(#"%hu", **(wingedEdges[i].indexPointer));
}
Tried looking at a few other problems with pointers and structs but I did not find anything. I am getting an error with the last NSLog call. Everything thing in the NSLog calls with indices and vertices is correct so it looks like it might be a simple syntax error or pointer issue. Also, how would I increment the pointer that indexPointer points to? Since indexPointer points to a indices pointer, then I want to access indices+1 and indices+2 as well through indexPointer.
(&indices+i) doesn't point to any memory you have allocated.
What will work is to change the indexPointer and vertex to single pointers and then
wingedEdges[i].indexPointer = &indices[i];
wingedEdges[i].vertex = &vertices[indices[i]];
Then *(wingedEdges[i].indexPointer) is the same as indices[i] and
wingedEdges[i].vertex->Position[0] is the same as vertices[indices[i]].Position[0]. However, you will not get the automatic updating that you want (see my comments for more details). I recommend a simple inline function:
inline *Vertex vertex(WingedEdge* e)
{
return &vertices[*(e->indexPointer)];
}
I'm assigning a CGFloat animatedDistance and I'm getting this error.
Here I'm assigning value to animatedDistance
static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3;
static const CGFloat LANDSCAPE_KEYBOARD_HEIGHT = 140;
heightFraction is CGFloat as well.
if (orientation == UIInterfaceOrientationPortrait ||
orientation == UIInterfaceOrientationPortraitUpsideDown)
{
animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);
}
else
{
animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction);
}
What type should be animatedDistace? Can someone help me?
floor returns a double. On some platforms, CGFloat is a float. animatedDistance should be typed as a double (you can cast it to a CGFloat if needed).
Use this to get a vector... CGpoint vector = ccpSub(cgpoint 1, cgpoint 2);
And if you want double/float values then do this:
CGpoint.location
for whatever you are trying to find the coordinates of, then assign a float to CGPoint.location.y and another float to CGPoint.location.x
You need cocos2d for this by the way. I think.
Sounds to me like you declared animatedDistance as holding some kind of pointer, such as NSNumber *, or a structure, such as CGSize. Either way, you can't assign a CGFloat there.
If animatedDistance holds an NSNumber object, create one around the value. Back when you asked this question, the way to do this was [NSNumber numberWithDouble:floor(…)]. Now, you can just use #(floor(…)).
If animatedDistance holds a CGSize or other structure, you're going to have to decide for yourself how to meaningfully convert from the single number you have to the kind of structure you want.
I'm using objective c and trying to output a value from a function. Apparently I am doing something wrong because I'm receiving an incorrect value.
This is my code:
-(float) getAngleBetween {
float num = 0.0;
return num;
}
and I'm calling it as follows:
float *theAngleBetween = [self getAngleBetween];
NSLog(#"Angle.. = %f", theAngleBetween);
Any help please?
float theAngleBetween = [self getAngleBetween];
// ^
There should be no *.
Since you are returning a float, the receiver should have type float as well. float* means a pointer to float, which is entirely different from float.
BTW, make sure you declare -(float)getAngleBetween; before you call [self getAngleBetween]. Put it in the #interface. If it is not declared before, the method will be assumed to have the type -(id)getAngleBetween;. On x86 returning a id and a float use different API (objc_msgSend vs objc_msgSend_fpret), which may be the cause of wrong result.
You should have:
float theAngleBetween = [self getAngleBetween];
Get rid of the *, that's for objects only, float is a primitive data type.