cyclingIsBetter
cyclingIsBetter

Reputation: 17591

NSMutable Array in other class

I declared an NSMutableArray in my .h and I set property and synthesize, but if i want use this array in other class? How I do?

Upvotes: 0

Views: 3806

Answers (2)

Hack Saw
Hack Saw

Reputation: 2781

The basic idea here is that in your original class, the array is referred to by a pointer. Your original class would allocate it and presumably load it. Other parts of your program can be handed the contents of the property, which is a pointer, assign that to their own pointer holder, and use it as if you had declared it there.

So if MyClass has a property of MyArray, which is an NSMutableArry *, then MyArray is a pointer holder, (just 'pointer' for short).

You program can then make a new pointer, like NSMutableArray *ThatArray, and then simply do:

MyClass *aClass = [[MyClass alloc] initWithMyInitStuff];
NSMutableArray *ThatArray = aClass.MyArray;

NSLog("Count of ThatArray: %d", [That.Array count]);

Upvotes: 2

Evan Mulawski
Evan Mulawski

Reputation: 55334

If you have, as you stated, created an @property for the NSMutableArray, you can easily access it in other classes by name. For example:

#import "MyClass.h"

...

MyClass *myClass = [[MyClass alloc]init];
NSMutableArray *array = [myClass.myArray mutableCopy];

(Note: I did not use memory management.)

Upvotes: 1

Related Questions