Just a quick newbie question here. I have a method that calculates a value and stores the result in a double variable, this variable is also a local variable to that method.I also have a second method that does a separate calculation but this method needs the result in the first. How can I get the value from the first method while still keeping that variable hidden to the rest of the class? Below is an example of what I'm trying to get at.
-(IBAction)methodA{
double answer;
answer = 2 + 3;
}
-(IBAction)methodB{
double answerTimeTwo;
answerTimeTwo = answer * 2; //Problem arises here as I cannot access "answer"
}
They shouldn't be decorated as actions unless they're the result of a UIControl event.
Do it like this:
- (double)methodA {
double answer = 2.0 + 3.0; // don't really need the stack variable, but it's okay
return answer;
}
- (double)methodB {
double answerTimesTwo = [self methodA] * 2.0;
return answerTimesTwo;
}
Related
ScreenShot of CodeCould someone please explain to me how everything works in this script except a simple int counter that I pass in as a parameter? However if I directly pass in the int counter field into the method instead of using/ref. the para, it works just fine, how is this even possible? HELP!
By default parameters you give to the function, are evaluated and its value is passed (eg. not int xy is passed but the value of int xy, so 5).
So if you change the value directly eg. CounterAi -= 1; you are just changing the value you've passed on not the underlying variable. So if you want to use Pass by Reference in these cases you must use out or ref.
If you change a parameter of the passed value however it's value will be changed without needing to use ref or out.
Example:
public void Example1(int myValue) {
// This won't change the actual variable, just the value of the parameter,
// that has been passed
myValue -= 1;
}
public void Example2(ref int myValue) {
// This will change the actual variable,
// it's changing just the value of the parameter again,
// but we're using pass by reference
myValue -= 1;
}
public void Example3(Transform finishLine) {
// This will change the actual variable,
// because it's changing the data within the object,
// that the parameter value refers to.
finishLine.position = flSpts[Random.Range(0, flSpots.Count)].position;
}
Ok so I -roughly- want this code:
test1.m:
Foo *foo = [[Foo alloc] init];
foo.x = 1.0f;
[staticClass bar:*foo.x];
staticClass.m:
-(void)bar:(float *)argVar
{
*argVar += 1.0f;
}
So I'm pointing the argVar to a property of the Foo class. Obivously the current code doesn't work.
What's the proper syntax for/way to do this?
I think that the proper way to do it is this:
float tmp = foo.x;
[staticClass bar:&temp];
foo.x = tmp;
and StaticClass.m should look like this:
+(void) bar:(float *) argvar // < not plus instead of minus, denotes static method
{
*argVar = 1.0f;
}
x is a property of Foo, not a variable. A property is just a short-hand for a pair of get/set methods. It has no address, as such, and so cannot be passed around as you are trying to do.
The simplest work-around is to go through a local variable:
float d = foo.x;
[staticClass bar:&d];
foo.x = d;
Also note that you use &, not *, to take the address of a variable.
Options:
change Foo such that it has a float * property
change +[staticClass bar:] such that it takes and returns a float, updating your client code accordingly
use ivar_getOffset to find the location of the instance variable backing x in your Foo instance
Hi I am trying to init an object with a double value in the format double filter[3][3];
but i keep getting the following error.
cannot convert 'double[3][3]' to 'double' in assignment.
in my header i have this
#interface filter : NSObject
{
double **matrix;
}
#property(nonatomic)double **matrix;
-(id)initWithMatrix:(double**)filterMatrix;
inside my class i have this method.
-(id)initWithMatrix:(double**)filterMatrix
{
matrix = filterMatrix;
return self;
}
and i am trying to call it like this.
double filter[3][3] = {0,0,0,0,1,0,0,0,0};
MyMatrix *myMatrix = [[MyMatrix alloc] initWithMatrix:filter];
I now get the error.
Error: Cannot convert double[*][3] to double** in argument passing
Any help on this issue would be amazing.
Thanks
A
That's because double** isn't the equivalent of double[*][*]. In fact, double[*][*] is an invalid type, because it leaves the stride undefined. double** is a pointer to a pointer to a double, or to put it another way, it's a pointer to an array of doubles. You should just use double* as your type.
Edit: To clarify, double[*][3] is still just an array of doubles, even though it has 2 dimensions. This is still the equivalent of double*.
A two-dimensional array is not the same thing as a pointer-to-a-pointer. You have two choices - change the filter class to contain a 2D array, or change your initialization to use pointer-to-pointers.
In choice #1, you're could keep a copy of the array in your filter instance, instead of just holding a pointer. You need to change the class interface:
#interface filter : NSObject
{
double matrix[3][3];
}
-(id)initWithMatrix:(double[3][3])filterMatrix;
Then your implementation of initWithMatrix: can just do a memcpy() or the equivalent to copy the data into your instance.
Choice #2 is a bit different. Keep your other code the way it is, but change your initialization of filter:
double row0[3] = {0,0,0};
double row1[3] = {0,1,0};
double row2[3] = {0,0,0};
double **filter[3] = { row0, row1, row2 };
It's probably safer to malloc() all of those arrays, since otherwise you're going to end up with references to stack variables in your filter class, but I think you get the idea.
you are passing as a parameter a double 2d array(double[][]) when your method signature asks for a double (a primitive like 34.2).
set the method declaration to
- (id)initWithMatrix:(double*) matrix;
this passes a pointer to your array (2d) to the method.
edit: missed a semicolon.
Hi I am trying to init an object with a double value in the format double filter[3][3];
but i keep getting the following error.
cannot convert 'double[3][3]' to 'double' in assignment.
in my header i have this
#interface filter : NSObject
{
double **matrix;
}
#property(nonatomic)double **matrix;
-(id)initWithMatrix:(double**)filterMatrix;
inside my class i have this method.
-(id)initWithMatrix:(double**)filterMatrix
{
matrix = filterMatrix;
return self;
}
and i am trying to call it like this.
double filter[3][3] = {0,0,0,0,1,0,0,0,0};
MyMatrix *myMatrix = [[MyMatrix alloc] initWithMatrix:filter];
I now get the error.
Error: Cannot convert double[*][3] to double** in argument passing
Any help on this issue would be amazing.
Thanks
A
That's because double** isn't the equivalent of double[*][*]. In fact, double[*][*] is an invalid type, because it leaves the stride undefined. double** is a pointer to a pointer to a double, or to put it another way, it's a pointer to an array of doubles. You should just use double* as your type.
Edit: To clarify, double[*][3] is still just an array of doubles, even though it has 2 dimensions. This is still the equivalent of double*.
A two-dimensional array is not the same thing as a pointer-to-a-pointer. You have two choices - change the filter class to contain a 2D array, or change your initialization to use pointer-to-pointers.
In choice #1, you're could keep a copy of the array in your filter instance, instead of just holding a pointer. You need to change the class interface:
#interface filter : NSObject
{
double matrix[3][3];
}
-(id)initWithMatrix:(double[3][3])filterMatrix;
Then your implementation of initWithMatrix: can just do a memcpy() or the equivalent to copy the data into your instance.
Choice #2 is a bit different. Keep your other code the way it is, but change your initialization of filter:
double row0[3] = {0,0,0};
double row1[3] = {0,1,0};
double row2[3] = {0,0,0};
double **filter[3] = { row0, row1, row2 };
It's probably safer to malloc() all of those arrays, since otherwise you're going to end up with references to stack variables in your filter class, but I think you get the idea.
you are passing as a parameter a double 2d array(double[][]) when your method signature asks for a double (a primitive like 34.2).
set the method declaration to
- (id)initWithMatrix:(double*) matrix;
this passes a pointer to your array (2d) to the method.
edit: missed a semicolon.
I am having a really odd problem trying to set a simple float value to 1.
My property:
{
float direction;
}
#property(nonatomic)float direction;
Which is synthesized:
#synthesize direction;
I then used the following code:
- (void)setDirection:(float)_direction {
NSLog(#"Setter called with value of %f",_direction);
self->direction = _direction;
}
For testing purposes...
When I try to change the value with this,
[[w getCharacter] setDirection:1.0f];
where [w getCharacter] gives this:
return [[[self scene] gameLayer] player];
I get the warning, "setDirection not defined."
If I switch to dot notation([w getCharacter].direction), I get "confused by earlier errors, bailing out".
Here is where the weirdness starts. When I try to change the value, the debug message displays _direction = 0.00000. When I check the number later, its still 0.000. I am clearly trying to change it to 1, why is this not working?
The simplest explanation is that [w getCharacter] doesn't return the class of object you think it does. Only the class that has direction defined for it can respond to the message. You should test this by explicitly calling it with the class it defined for.
It is possible you did not include the header that defines the method.
Two probably unrelated issues:
The self->direction construction will work for a scalar value but it does an end run around the entire class concept. In this case just use: 'direction=_direction;` and it will set it directly.
Apple reserves all names that start with underscores for its own internal use. You should not use them because Objective-c has a global name space. It's possible that you can accidentally use an Apple variable that is defined deep within a framework. (This is why framework constants all start with NS,CF,CA etc.)
[Note: In the Comments, the author says to ignore this answer.
self.direction = 1; is syntactic sugar for
[self setDirection: 1];
when you call
-(void)setDirection:(float)_newDirection {
self.direction = _newDirection;
}
You seem to be telling the compiler or preprocessor to set up a recursive loop for you. The preprocessor (I think) changes it to this:
-(void)setDirection:(float)_newDirection {
[self setDirection: _newDirection];
}
If you call it simply
-(void)setDirection:(float)_newDirection {
direction = _newDirection;
}
the assignment should work (it worked for me just now)