Saturday, 24 August 2013

Objective-C: Accessing int value from another class

Objective-C: Accessing int value from another class

I am having trouble accessing an int property from another class. I know
this question has been asked quite a few times however none of the
solutions posted in previous questions seem to work. My knowledge in xcode
is basic, and I am using this project to develop my skills.
The two classes I have are: HelloWorldLayer and ClassOne. Where ClassOne
states the value of int. Both are Cocos2d CCLayer classes (probably not
the best class to practice inter-class value access).
ClassOne.h
@interface ClassOne : CCLayer {
int ageClass;
}
@property (nonatomic, readwrite)int ageClass;
@end
ClassOne.m
@implementation ClassOne
@synthesize ageClass = _ageClass;
-(id)init{
if((self=[super init])){
_ageClass = 10;
}
return self;
}
@end
HelloWorldLayer.h
#import "ClassOne.h"
@interface HelloWorldLayer : CCLayer <...> {
ClassOne *agePointer;
}
@property (nonatomic,assign)ClassOne *agePointer;
+(CCScene*)scene;
@end
HelloWorldLayer.m
#import "HelloWorldLayer.h"
#import "AppDelegate.h"
#import "ClassOne.h"
@implementation HelloWorldLayer
@synthesize agePointer = _agePointer;
+(CCScene*)scene...
-(id)init{
if((self=[super init])){
_agePointer.ageClass = self;
NSLog(@"ClassOne int = %@",_agePointer);
}
return self;
}
...
@end
Output Result:
"ClassOne int = (null)"
or "0" if i use "%d" token and "int = x", where the line "int x
=_agePointer.ageClass;"
is used.
The result I am after is for the HelloWorldLayer NSLog to display "10",
the int value defined in ClassOne.
Any wisdom and corrections on my use of language is greatly appreciated.

No comments:

Post a Comment