0

I am trying to pass a NSMutableArray between UIViewController and a class.

I have a Class:

@interface uClass : NSObject<>{
  NSMutableArray *arr;
}

@property (nonatomic,retain) NSMutableArray *arr;
@synthesize arr;

-(void)getData{
arr = [[NSMutableArray alloc]initWithCapacity:10];

[arr addObject:@"1.3"];
[arr addObject:@"2.9"];

}

in the UIViewCOntroller, In MapScreen.h,

#include "uClass.h"
@interface MapScreen : UIViewController<>{
   NSMutableArray *cor;
}
In MapScreen.m,


-(void)setPosition{
uClass *u = [[uClass alloc]init];
cor =[[NSMutableArray alloc]initWithCapacity:10];
cor = u.arr;
}

When i try to obtain the first object [cor objectAtIndex:0], it is null. Could someone please tell me whats happening??

Ok an update on what exactly i am doing,

The uClass contains a thread which runs continously in the background, it started by the click of a button. During this process getData is called and arr NSMutableArray is populated.

Now when i move to MapScreen UIViewController and click on a button, it should get the NSMutableArray arr from uClass. But when i do that it gives a null. I hope this is more clearer.

6
  • Are you adding objects to the array in the initialiser? Commented Aug 30, 2011 at 13:26
  • No i add in another function in the uClass. Commented Aug 30, 2011 at 13:27
  • So when do you think the array objects are being added? All your last code snippet does is alloc, init the class and try and get the array. Your method to populate the array isn't getting called. Commented Aug 30, 2011 at 13:31
  • Well the getData is a function that runs in a background process and gets populated every few seconds. From the UI MapScreen i call SetPosition, which gets the NSMutableArray. Commented Aug 30, 2011 at 13:38
  • See - there is a lot of extra data that you aren't putting in your question and I'm having to probe to find out. Commented Aug 30, 2011 at 13:54

4 Answers 4

1
-(void)setPosition{
uClass *u = [[uClass alloc]init];

So far you created a new instance of uClass, stored in u. You initialize it but you do not tell us what happens in init. (in a comment you mentioned that you do not do anything with arr in it.)

arr is most probably nil (!)

cor =[[NSMutableArray alloc]initWithCapacity:10];

This creates an empty array. Now you point in cor to an empty array initialized with a capacity of 10. Why?

cor = u.arr;

And here you discard the empty and initialized array that you stored in cor and let cor point to the content of arr instaed. But arr probably points to nowhere.

So there is at least one mistake. There is no point in allocating and initializing an array in for cor when you overwrite cor with the contents of arr in the very next step.

The other potential mistake is that you did not assign anything to arr in the init method of the class uClass or you may want to replace the initialization of cor with

[u getData];

or so

}

Try:

-(void)setPosition{
uClass *u = [[uClass alloc]init];
[u getData];
cor = u.arr;
}

Edit: Sorry I just read a comment to the question above where you tell that getDate gets called in the background initially.

In that case you must not create a new empty array in there. if you assigned cor to arr so that both variables point to the same array object then you must not assing any of those with any other (newly created) object. Because if you do so then you loose the link in between.

Please take this suggestion for getData:

-(void)getData{
[arr removeAllObjects];

[arr addObject:@"1.3"];
[arr addObject:@"2.9"];
}

However, arr still needs to be allocated and initilized once. The init method of uClass may be a good place for that.

-(void) init {
  [super init];
  arr = [[NSMutableArray alloc]initWithCapacity:10];
}

Your new getPosition would simply be:

-(void)setPosition{
uClass *u = [[uClass alloc]init];
cor = u.arr;
}

because there is no need to initialize cor. Just assign the (initialized) value of arr.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi thanks alot for your detailed explanation, i tried what you suggested, but i still seem to be gettin the same result. One more thing, initialising arr in init, wont that get re initialized when i create an object of uClass in setPosition, and thus clearing all the values.
Post your updated code when you want help on that. Re your question. Of course the array arr would be initialzed (and therefore emptied) every time when you create a new instance of a class. Every new instance of that class is an independent object which has its own arr property. If that is not what you want then you could create only one instance of uClass or use a class property instead of an instance property. (The latter suggestion may be too advanced yet, considering the nature of your question.) That all depends on what you really want to achieve.
0

Probably u.arr returns nil or empty array.

Also [[NSMutableArray alloc]initWithCapacity 10]; returns empty array.

Comments

0

Maybe you could try to override the arr getter to return the pure mutable array :

- (NSMutableArray*) arr { return arr; }

Because you get a NSArray with the synthesied getter, and maybe it's possible that you do not get a pointer toward the original array, but a pointer to a copy of it, taht is empty at the moment you request for it.

Comments

0

just for anyone who is searching for a solution to this problem, i used Singleton class to solve this issue, an example to refer to: http://www.galloway.me.uk/tutorials/singleton-classes/

BR,

Suppi

1 Comment

Please don't use singleton classes unless you know what you're doing.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.