Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jun 25, 2011, 05:42 AM   #1
PluueeR
macrumors newbie
 
Join Date: Oct 2008
[Declared properties] Passing variables between classes

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?
PluueeR is offline   0 Reply With Quote
Old Jun 25, 2011, 05:59 AM   #2
gnasher729
macrumors G5
 
gnasher729's Avatar
 
Join Date: Nov 2005
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.
gnasher729 is offline   0 Reply With Quote
Old Jun 25, 2011, 06:02 AM   #3
jiminaus
macrumors 65816
 
Join Date: Dec 2010
Location: 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.
jiminaus is offline   0 Reply With Quote
Old Jun 25, 2011, 08:16 AM   #4
PluueeR
Thread Starter
macrumors newbie
 
Join Date: Oct 2008
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.
PluueeR is offline   0 Reply With Quote
Old Jun 25, 2011, 08:44 AM   #5
jiminaus
macrumors 65816
 
Join Date: Dec 2010
Location: Sydney
Quote:
Originally Posted by PluueeR View Post
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];

    [newTest1 setTest0:test0];
    [newTest1 opvragen];
	
    [pool drain];
    return 0;
}
Test1.h
Code:
Code:
#import <Cocoa/Cocoa.h>
#import "Test0.h"


@interface Test1 : NSObject {
    Test0 *test0;
}

@property (retain) Test0 *test0;

- (void)opvragen;

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

@implementation Test1

@synthesize test0;

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

}

@end
jiminaus is offline   0 Reply With Quote
Old Jun 25, 2011, 08:45 AM   #6
gnasher729
macrumors G5
 
gnasher729's Avatar
 
Join Date: Nov 2005
Quote:
Originally Posted by PluueeR View Post
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?
gnasher729 is offline   0 Reply With Quote
Old Jun 25, 2011, 10:47 AM   #7
PluueeR
Thread Starter
macrumors newbie
 
Join Date: Oct 2008
@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.
PluueeR is offline   0 Reply With Quote
Old Jun 25, 2011, 11:21 AM   #8
gnasher729
macrumors G5
 
gnasher729's Avatar
 
Join Date: Nov 2005
Quote:
Originally Posted by PluueeR View Post
@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.
gnasher729 is offline   0 Reply With Quote
Old Jun 26, 2011, 04:26 AM   #9
PluueeR
Thread Starter
macrumors newbie
 
Join Date: Oct 2008
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.
PluueeR is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Passing variables from Applescript to bash urello Mac Programming 2 Mar 6, 2014 11:53 PM
Instance Variables vs. Properties ahan.tm iPhone/iPad Programming 34 Oct 27, 2013 05:00 PM
Internet Cafes Declared Illegal in Ohio IGregory Politics, Religion, Social Issues 39 May 29, 2013 03:02 PM
Passing Variables to another View Controller mngoose iPhone/iPad Programming 5 Oct 6, 2012 05:03 AM

Forum Jump

All times are GMT -5. The time now is 09:19 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC