0

I have two classes, Subject Class and Chapter Class.

I have assigned a NSMutable array in Subject class like

@property(nonatomic,retain)NSMutableArray *htmlURL;

in .m file:

htmlURL = [[NSMutableArray alloc]init];
[htmlURL addObject:@"1", @"2"];

In Chapter Class i done like this,

Subject *sub = [[Subject alloc] init];
NSLog(@"%@"sub.htmlURL);

The output is null... i want the htmlurl array values in Chapter Class.. What i'm missing here..

3
  • you have to show more code, where in the .m file do you alloc/init the array? in the init method? which os are you targeting? Commented Apr 2, 2013 at 9:24
  • I'm parsing XML so in parserDidStartDocument method i'm alloc/init the array.. Commented Apr 2, 2013 at 9:26
  • is subject parent of chapter? Commented Apr 2, 2013 at 9:31

1 Answer 1

1

You have to connect both these classes!!!!

You are creating two different objects and then trying to access value of one object's member in another object. Do this.

While you are coming to Chapter Class from Subject Class, you might be pushing or presenting the chapter class.

//in Subject Class, while creating Chapter class

Chapter* newClass = [Chapter alloc] init];
newClass.parentObject = self;
//code for transitioning to chapter controller

// in Chapter Class, create a parent object
@property (nonatomic, assign) Subject* parentObject;

OR if SUBJECT is a generalized common class,

then you could create a method to return a common instance of it. Something like this,

+ (Subject *) sharedInstance
{
    @synchronized([Subject class]) {
        if (!instance) {
            instance = [[Subject alloc] init];
        }
    }    
    return instance;
}

And then access Subject Class members as,

Subject *sub = [Subject sharedInstance];
NSLog(@"%@"sub.htmlURL);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.