2

MasterViewController.h:

#import <UIKit/UIKit.h>

@interface MasterViewController : UITableViewController

@property (nonatomic, strong) NSMutableArray *listOfBugs;

MasterViewController.m:

- (void)viewDidLoad
{
    [super viewDidLoad];
    _listOfBugs = [[NSMutableArray alloc]init];
}

...
...

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{
    ScaryBugDoc *bug = [self.bugs objectAtIndex:indexPath.row];
    [_listOfBugs addObject:bug];
}

In the above code I've created an array that holds objects with properties. I want to access the objects inside the array from a different class and also add objects to the same array. I know I can create a new array from the same class like this:

ListOfBugsViewController.m:

#import "ListOfBugsViewController.h"
#import "MasterViewController.h"
MasterViewController *myClass = [[MasterViewController alloc]init];


    NSMutableArray *array = [myClass.listOfBugs....

But obviously that's not what I'm looking for. I want to access the objects that are already inside the array and not to create a new array.

Edit: To make things a bit simpler, I have a few classes and only one array I'm adding objects to. All classes should be able to add objects to it and read previously added objects. How can I access that same array from a class it wasn't allocated and initialized in?

Thanks

4 Answers 4

1

Adding an object to an array from other class in fairly simple.

Just see how...

Base.m

...
_baseArray=[[NSMutableArray alloc]init];
[_baseArray addObject:@"Anoop"];
[_baseArray addObject:@"kumar"];
...

OtherClass.m

Base *baseObj=[Base new];
[baseObj.baseArray addObject:@"vaidya"];

EDIT:

As per our discussion

- (void)awakeFromNib
{
    [super awakeFromNib];
    [self createObjects];

}

- (void) createObjects
{
    _listOfBugs=[[NSMutableArray alloc]init];

    [_listOfBugs addObject:@"Anoop"];

    NSLog(@"init done, %@",_listOfBugs);

}


- (id)init
{
    self = [super init];    
    if (self) {
        [self createObjects];
    }
    return self;
}
Sign up to request clarification or add additional context in comments.

7 Comments

MasterViewController *baseObj=[MasterViewController new]; [baseObj.listOfBugs addObject:@"vaidya"]; [baseObj.listOfBugs addObject:@"test"]; NSLog(@"this is %@",([baseObj.listOfBugs objectAtIndex:0])); I'm getting : ScaryBugs[2224:11303] this is (null) . Did I do something wrong?
in MasterViewController did you alloc+init listOfBugs in init method?
you are doing it in viewDidLoad, check by putting the above in init()
in MasterViewController I alloc+init listOfBugs inside viewDidLoad. Where is init() ?
create a method as : - (id)init { self = [super init]; if (self) { _baseArray=[[NSMutableArray alloc]init]; [_baseArray addObject:@"Anoop"]; [_baseArray addObject:@"kumar"]; } return self; }
|
0

But obviously that's not what I'm looking for. I want to access the objects that are already inside the array and not to create a new array.

You're not creating a new array, you're creating a new pointer to an existing array. You can edit that array via this new pointer.

NSMutableArray *array = myClass.listOfBugs;
[array addObject:newBug];

However you don't have to create a new pointer, you can access the array directly via the property:

[myClass.listOfBugs addObject:newBug];

3 Comments

so [myClass.listOfBugs addObject:newBug]; is the only line I need to add to the new class without allocating and init it?
Your question is a little confusing, but I think so yes.
I'll edit my first post now but adding just the above line of code gives me the error "Property 'listOfBugs' not found on object of type 'MasterViewController'"
0

Your question is confusingly worded. I think what you want is to access the listOfBugs property from a previously created instance of MasterViewController from your ListOfBugsViewController.

Is your ListOfBugsViewController being instantiated by your MasterViewController? If so you could add a new property to your ListOfBugsViewController to contain a pointer to the MasterViewController (make this property weak) and then have your MasterViewController set that pointer to itself right after it instantiates your ListOfBugsViewController.

1 Comment

I've edited the first post. The code of the above can really help.
0

The nice thing about Objective C is the objects are actually pointers to object instances. What's even nicer is when you pass an object, it's passed by reference (correct me if I am wrong).

You can simply call a method from your other class and pass in your source mutable array like so:

// ------------------------------------------
// Your "Other Class" .h header file
// ------------------------------------------

...

@interface OtherClass
{
    ...
}

...

+(void)addNewBugToArray:(NSMutableArray *)paramSourceArray;

@end

// ------------------------------------------
// Your "Other Class" .m implementation file
// ------------------------------------------

...

// ------------------------------------------
// Note: paramSourceArray is passed 
// by reference, meaning any change to it
// will affect the source array directly
// ------------------------------------------
+(void)addNewBugToArray:(NSMutableArray *)paramSourceArray
{
    [paramSourceArray addObject:@"newBug"];
}

...

@end




// ------------------------------------------
// Your Main View Controller .m file
// ------------------------------------------

#import "OtherClass.h"

...

-(void)viewDidLoad
{
   ...

   // initialize your source array
   sourceArray = [[NSMutableArray alloc] init];

   [btnAddBug addTarget:self selector:@selector(addNewBug) forControlEvent:UIControlEventTouchUpInside];

   ...

}

-(void)addNewBug
{
    // ---------------------------------------------------------
    // calling class method from "OtherClass" to add new bug
    // to our source array directly using pass by reference
    // ---------------------------------------------------------
    [OtherClass addNewBugToArray:sourceArray];
}

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.