Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

PluueeR

macrumors newbie
Original poster
Oct 1, 2008
8
0
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
Code:
#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
Code:
#import <Cocoa/Cocoa.h>

@interface Test0 : NSObject {
	int iVar;
}

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

Test0.m
Code:
#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
Code:
#import <Cocoa/Cocoa.h>
#import "Test0.h"


@interface Test1 : NSObject {

}

- (void)opvragen;

@end

Test1.m
Code:
#import "Test1.h"

@implementation Test1

- (void)opvragen {
	
	Test0 *test0 = [[Test0 new] autorelease];
	//	test0.iVar = 5;
	
	NSLog(@"Class Test1: %i", test0.iVar);

}

@end

The output:
Code:
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

Suspended
Nov 25, 2005
17,980
5,565
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

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

and you'll see there are different objects.
 

jiminaus

macrumors 65816
Dec 16, 2010
1,449
1
Sydney
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

macrumors newbie
Original poster
Oct 1, 2008
8
0
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

macrumors 65816
Dec 16, 2010
1,449
1
Sydney
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
Code:
#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];

    [COLOR=blue][newTest1 setTest0:test0];[/COLOR]
    [newTest1 opvragen];
	
    [pool drain];
    return 0;
}

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


@interface Test1 : NSObject {
    [COLOR=blue]Test0 *test0;[/COLOR]
}

[COLOR=blue]@property (retain) Test0 *test0;[/COLOR]

- (void)opvragen;

@end

Test1.m
Code:
#import "Test1.h"

@implementation Test1

[COLOR=blue]@synthesize test0;[/COLOR]

- (void)opvragen {
		
	NSLog(@"Class Test1: %i", test0.iVar);

}

@end
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,565
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

macrumors newbie
Original poster
Oct 1, 2008
8
0
@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

Suspended
Nov 25, 2005
17,980
5,565
@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

macrumors newbie
Original poster
Oct 1, 2008
8
0
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.