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
Test0.h
Test0.m
Test1.h
Test1.m
The output:
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?
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?