Reputation: 450
I have just started studying Objective - C..and I encountered a problem which I have been trying to solve for hours now..Here's my code..
ClassA.h
#import <Foundation/Foundation.h>
// The ClassA Class
// @interface section
@interface ClassA: NSObject
{
NSMutableArray *x;
}
@property (copy,nonatomic) NSMutableArray *x;
- (void) initVar;
- (void) print;
- (void) addStr: (NSString *) str;
@end
ClassA.m
#import "ClassA.h"
// @implementation section
@implementation ClassA
@synthesize x;
- (void) initVar
{
x = [[NSMutableArray alloc] init];
}
- (void) print
{
for(NSString *str in x)
NSLog (@" --------- %@", str);
}
- (void) addStr: (NSString *) str
{
if(![x containsObject: str])
[x addObject: str];
}
@end
ClassB.h
#import "ClassA.h"
// The ClassB Class
// @interface section
@interface ClassB: ClassA
{
NSMutableArray *y;
}
- (void) initVar;
- (void) print;
- (void) addStr: (NSString *) str;
@end
ClassB.m
#import "ClassB.h"
// @implementation section
@implementation ClassB
- (void) initVar
{
y = [[NSMutableArray alloc] init];
}
- (void) print
{
for(NSString *str in y)
NSLog (@" />>>>>>>>> %@", str);
}
- (void) addStr: (NSString *) str
{
if(![self.x containsObject: str] && ![y containsObject: str])
{
[self.x addObject: str];
[y addObject: str];
}
else if([self.x containsObject: str] && ![y containsObject: str])
{
[y addObject: str];
}
}
@end
Here's where I'm calling the classes
#import "./MyClasses/ClassB.h"
int main(int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSLog (@"This will print!");
ClassB *b = [[ClassB alloc] init];
ClassA *a = [[ClassA alloc] init];
[a initVar];
[a addStr: @"obj1"];
[a addStr: @"obj2"];
[a addStr: @"obj3"];
[a print];
[b initVar];
[b addStr: @"YOH1"];
[b addStr: @"YOH2"];
[b addStr: @"YOH3"];
[b print];
NSLog (@" +++++++++++++++++++++++++++++++++ ");
[a print];
[a release];
[b release];
[pool drain];
return 0;
}
I want it's output is like this..
This will print! ------------ obj1
------------ obj2
------------ obj3
/>>>>>>>>>>>> YOH1
/>>>>>>>>>>>> YOH2
/>>>>>>>>>>>> YOH3
++++++++++++++++++++++++++++++++++++
------------ obj1
------------ obj2
------------ obj3
------------ YOH1
------------ YOH2
------------ YOH3
Currenty the last 3 output ( YOH1, YOH2 and YOH3) aren't showing. Meaning that I didn't add the object to x
successfully. What am I doing wrong?
Upvotes: 0
Views: 1818
Reputation: 11552
But you have not added YOH1, YOH2 and YOH3 to a
, have you?
Additionally, x
will not be initialized for classB.
Upvotes: 0
Reputation: 17143
So the 'b' object doesn't even have an NSMutableArray in 'x'. 'x' will be nil for the object 'b'.
But I suspect there's a different misunderstanding here. The object 'b' and the object 'a' are two different objects. Just because class B is a subclass of A, doesn't mean that adding strings to some instance of class B is going to add those strings to some other instance of class A.
To answer your question in the comments, suppose you did want every instance of class B to have its own NSMutableArray in 'x'. You can do this like so:
// in ClassA.h
@interface ClassA: NSObject {
NSMutableArray *x;
}
@property (retain,nonatomic) NSMutableArray *x;
@end
// in ClassA.m
@implementation ClassA
@synthesize x;
- (id)init {
if ((self = [super init])) {
x = [[NSMutableArray alloc] init];
// whenever we init an instance of class A, we will have
// an nsmutablearray in 'x'
}
return self;
}
- (void)dealloc {
// assume you aren't using ARC
[x release];
[super dealloc];
}
// in ClassB.h
@interface ClassB: ClassA {
NSMutableArray *y;
}
@property (retain,nonatomic) NSMutableArray *y; // for consistency
@end
// in ClassB.m
@implementation ClassB
@synthesize y;
- (id)init {
if ((self = [super init])) { // call Class A's init
y = [[NSMutableArray alloc] init];
// whenever we init an instance of class B, we will have
// everything an instance of A has, plus this y we need
}
return self;
}
- (void)dealloc {
// assume you aren't using ARC
[y release];
[super dealloc];
}
I moved your initVar into a proper init, there's no reason to have two methods there. And I'm just showing the init's only, leaving out the addString and print.
So now you can do this:
ClassB *instanceOfB = [[ClassB alloc] init];
and that instance will have both the x and y arrays that you want.
I left the instance variable declarations in there also, even though they aren't strictly required with the newer compilers. The newer compiler versions can implicitly define those instance variables. If you don't understand that, don't worry about it at this point. Not that important.
I hope that helps.
Upvotes: 3