PDA

View Full Version : [Declared properties] Passing variables between classes




PluueeR
Jun 25, 2011, 05:42 AM
I'm new to Objective-C and Object Oriented Programming and I'm working on my very first app. I'm testing with passing variables between classes and I understand that I need to use declared properties. I followed a couple of tutorials on the subject but I can't seem to let it work. Here is my code:

Properties Test.m

#import "Test0.h"
#import "Test1.h"

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

Test0 *test0 = [[Test0 new] autorelease];

NSLog(@"Main: %i", test0.iVar);

[test0 setIVar: 6];

Test1 *newTest1 = [Test1 new];

[newTest1 opvragen];

[pool drain];
return 0;
}


Test0.h

#import <Cocoa/Cocoa.h>

@interface Test0 : NSObject {
int iVar;
}

@property (readwrite,assign) int iVar;
@end


Test0.m

#import "Test0.h"

@implementation Test0
@synthesize iVar;

- (id) init {
if (self = [super init]) {

[self setIVar: 5];

NSLog(@"Class Test0: %i", iVar);
}

return self;
}
@end


Test1.h

#import <Cocoa/Cocoa.h>
#import "Test0.h"


@interface Test1 : NSObject {

}

- (void)opvragen;

@end


Test1.m

#import "Test1.h"

@implementation Test1

- (void)opvragen {

Test0 *test0 = [[Test0 new] autorelease];
// test0.iVar = 5;

NSLog(@"Class Test1: %i", test0.iVar);

}

@end


The output:

2011-06-25 12:37:54.505 Properties Test[420:a0f] Class Test0: 5
2011-06-25 12:37:54.514 Properties Test[420:a0f] Main: 5
2011-06-25 12:37:54.517 Properties Test[420:a0f] Class Test0: 5
2011-06-25 12:37:54.518 Properties Test[420:a0f] Class Test1: 5


What I'm doing wrong? It seems after main (which set the iVar to 6), class Test0 is loaded again and resets it to 5. Right?

In this case I use an int as the property. If you use an NSObject as the property is it possible to use it's methods? Or can you only use it's getter and setter?



gnasher729
Jun 25, 2011, 05:59 AM
1. Avoid using "new". It makes it hard to find where objects are allocated, because everyone uses the [[... alloc] init] pattern.

2. The output of your code does exactly what your code does. I think you may be confused between classes and instances of classes aka objects. You create two instances of the class Test0 in two different places. One has iVar set to 6 after you print it, the other has iVar set to 5 in the "init" method. To see what happens, change the NSLog statements to

NSLog (@"Object %@: iVar = %d", test0, test0.iVar);

and you'll see there are different objects.

jiminaus
Jun 25, 2011, 06:02 AM
You have two seperate instances of Test0, one in main and one in Test1.

In main you construct a Test0 object whose pointer is assigned to the test0 local variable in main. This object's iVar property is then set to 6.

Afterwards to you construct a Test1 object whose pointer is assigned to the test1 local variable in main. You let send the opvragen message to the new Test1 object. In Test1's opvragen method, a second Test0 object is created and assigned to test0 local variable in that method.

Classes are just templates. What you actually deal with are objects, which are instances of classes. Each object's instance variables are separate from each other.

We are both instances of the Human class, for example. A Human class might have name and age instance variables. But while we both have a name and an age, we would have different, separate values stored in those instance variables.

The same is happening here.

PluueeR
Jun 25, 2011, 08:16 AM
I get it!

In my actual program I create an object which I use to store some values in a array (it has methods for adding, deleting values and for doing some math with it). But how can I use this same object from other objects? It's easy from the object that creates the object. But how do you call these methods (on the same data) from other objects?

Is this actually possible? I thought this could be done by using declared properties, but maybe I'm wrong. Guess the whole objects thing is a bit confusing for someone that's used to PHP and Posix scripting :)

But I'm learning, I've got Objective-C for Dummies and Cocoa Programming for Dummies. And I'm learning every day. But this is something, I can't seem to get right.

Thanks for helping me out btw.

jiminaus
Jun 25, 2011, 08:44 AM
But how can I use this same object from other objects? It's easy from the object that creates the object. But how do you call these methods (on the same data) from other objects?


The first object that creates the to-be-shared object gets a pointer to that object. It needs to somehow pass the pointer to the shared object to other object that wants to access it.

You can use declared properties to do this. Let's go back to original example, and change it so that Test1 has a pointer to the same Test0 object that main creates.

Properties Test.m

#import "Test0.h"
#import "Test1.h"

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

Test0 *test0 = [[[Test0 alloc] init] autorelease];

NSLog(@"Main: %i", test0.iVar);

[test0 setIVar: 6];

Test1 *newTest1 = [[[Test1 alloc] init] autorelease];

[newTest1 setTest0:test0];
[newTest1 opvragen];

[pool drain];
return 0;
}


Test1.h

Code:
#import <Cocoa/Cocoa.h>
#import "Test0.h"


@interface Test1 : NSObject {
Test0 *test0;
}

@property (retain) Test0 *test0;

- (void)opvragen;

@end


Test1.m

#import "Test1.h"

@implementation Test1

@synthesize test0;

- (void)opvragen {

NSLog(@"Class Test1: %i", test0.iVar);

}

@end

gnasher729
Jun 25, 2011, 08:45 AM
I get it!

In my actual program I create an object which I use to store some values in a array (it has methods for adding, deleting values and for doing some math with it). But how can I use this same object from other objects? It's easy from the object that creates the object. But how do you call these methods (on the same data) from other objects?

Let's say you have three variables

int x = 1;
int y = 2;
int z = 3;

How to you print them?

Next you have three objects

Test0* obj1;
Test0* obj2;
Test0* obj3;

How to you print their iVar values?

PluueeR
Jun 25, 2011, 10:47 AM
@jiminaus Thanks that seems to work. Now I'll try to expand to my application using an array, but logically it would be the same I guess.

@gnasher729 I don't know what you mean. I do realize now that you can't use the methods in the class that holds the data from other objects, but you need to create new methods.

gnasher729
Jun 25, 2011, 11:21 AM
@jiminaus Thanks that seems to work. Now I'll try to expand to my application using an array, but logically it would be the same I guess.

@gnasher729 I don't know what you mean. I do realize now that you can't use the methods in the class that holds the data from other objects, but you need to create new methods.

Sorry, but I can't understand what you mean either. Anyone can call class methods, and anyone having access to any object can call the instance methods of these objects.

PluueeR
Jun 26, 2011, 04:26 AM
Yeah I understand that. I can use a class in all my classes, but I can't seem to use an object (created in class 1) in class 2. Right? That's why I started reading about declared properties.