PDA

View Full Version : passing a value through an object to another object




larswik
Sep 3, 2010, 01:41 AM
I'm trying out a test from an example in the book I am learning from to better understand it. Example, in Main I am trying to pass an argument to Object A that then gets passed through to Object B. Then back in Main I am trying to print the value from Object B. I am not getting any errors but a warning saying " passing argument 1 of setXvalue: makes pointer from integer without a cast". This error shows up in Main with this line [passTo setXvalue: 10];. The value that is returned in the Console is 0 when it should be 10

Here is the code

#import <Foundation/Foundation.h>
#import "xholder.h"
#import "passer.h"

int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
xholder * holder = [[xholder alloc] init];
passer * passTo = [[passer alloc] init];

// setting the value 5 to int passit and print test
[passTo setPassit: 5];
NSLog(@"pass x = %i", passTo.passit);

[passTo setXvalue: 10];
NSLog(@"holder is %i", [holder print]);

[holder release];
[passTo release];
[pool drain];
return 0;
}
OBJECT A:
#import <Foundation/Foundation.h>
#import "xholder.h"

@interface passer : NSObject

{
int passit;
xholder *xvalue;
}
@property int passit;

-(void) setPassit: (int) a;
-(int) print;
-(void) setXvalue: (xholder *) b;

@end
#import "passer.h"


@implementation passer
@synthesize passit;

-(void) setPassit: (int) a
{
passit = a;
}
-(int) print
{
return passit;
}
-(void) setXvalue: (xholder *) b
{
xvalue = b;
}
@end
Object B:
#import <Foundation/Foundation.h>

@interface xholder : NSObject
{
int xvalue;
}
@property int xvalue;

-(void) setXvalue: (int) xVal;
-(int) print;
@end
#import "xholder.h"

@implementation xholder
@synthesize xvalue;

-(void) setXvalue: (int) xVal
{
xvalue = xVal;
}
-(int) print
{
return xvalue;
}

@end



robbieduncan
Sep 3, 2010, 02:48 AM
setXValue is declared as accepting a pointer to xholder (so an instance of the xholder class). You are passing an integer.

I would note that wherever you are learning from seems to be teaching you bad habits: in Cocoa class names are in TitleCase (as a convention).

larswik
Sep 3, 2010, 03:41 AM
It is a good book I am learning from, Programing in objective C 2.0. When I don't understand something I pick up bad (incorrect) habits that I am trying to fix. I am learning the 'Inheritance' section now and trying out my own versions of it so I can better understand how pointers work. I am trying to pass the argument which is an integer 5 through the method deceleration setXValue located in the 'passer' class. That decoration has a pointer in it where I am trying to further send the integer 5 to the xholder class (object) to set that instance variable 'xvalue' to 5.

I have spent the last 3 hours trying to solve the problem (learning) myself. If you could, please, Can you show me how to correct the bad code so I can understand what is happening.

Thanks you!

robbieduncan
Sep 3, 2010, 04:16 AM
It is a good book I am learning from, Programing in objective C 2.0. When I don't understand something I pick up bad (incorrect) habits that I am trying to fix. I am learning the 'Inheritance' section now and trying out my own versions of it so I can better understand how pointers work. I am trying to pass the argument which is an integer 5 through the method deceleration setXValue located in the 'passer' class. That decoration has a pointer in it where I am trying to further send the integer 5 to the xholder class (object) to set that instance variable 'xvalue' to 5.

I have spent the last 3 hours trying to solve the problem (learning) myself. If you could, please, Can you show me how to correct the bad code so I can understand what is happening.

Thanks you!

1) you are not trying to send the integer 5 to the xholder class. You may well be trying to send a message to an instance of the class that will set a value to 5. It is dangerous to start thinking that the class and instances of the class are the same things. The class itself is an object as are the instances of it.

2) If you are trying to send a message to the instance of the xholder class pointed to by the holder variable to set the value to 5 then why are you sending the message to the variable passTo that points to an instance of passer?

To summarise:
[passTo setXvalue: 10];
should be
[holder setXvalue: 10];

larswik
Sep 3, 2010, 04:49 AM
robbieduncan..... Thanks you for pointing out that very important point, I think it clicked. I assumed I was trying to pass the integer 5 all the way to the instance variable in xholder class. But instead what is happening is I am sending a message (like an email) to the instance variable to 'set it's self to 5'. Is this the case for most everything? You don't pass things a long, instead you send messages for them you are now the value of 5?

For my syntax (grammar), I say, I CREATE a new class where I set up INSTANCE VARIABLES and DECLARE it's methods. In the IMPLEMENTATION section I DEFINE the METHODS for the class. In MAIN I create OBJECTS, which can also be called an INSTANCE, from my classes to work with. So an OBJECT, or INSTANCE is kind of a copy of the CLASS. Doing so allows me to send and receive MESSAGES from OBJECTS.

Is my understanding correct?

Thanks again very much. It is a struggle for me and even more difficult if I am learning incorrectly :)

robbieduncan
Sep 3, 2010, 05:37 AM
robbieduncan..... Thanks you for pointing out that very important point, I think it clicked. I assumed I was trying to pass the integer 5 all the way to the instance variable in xholder class. But instead what is happening is I am sending a message (like an email) to the instance variable to 'set it's self to 5'. Is this the case for most everything? You don't pass things a long, instead you send messages for them you are now the value of 5?

For my syntax (grammar), I say, I CREATE a new class where I set up INSTANCE VARIABLES and DECLARE it's methods. In the IMPLEMENTATION section I DEFINE the METHODS for the class. In MAIN I create OBJECTS, which can also be called an INSTANCE, from my classes to work with. So an OBJECT, or INSTANCE is kind of a copy of the CLASS. Doing so allows me to send and receive MESSAGES from OBJECTS.

Is my understanding correct?

Thanks again very much. It is a struggle for me and even more difficult if I am learning incorrectly :)

I can't see anything logically wrong with this, although I might phrase it differently.