开发者

passing NSMutableArray between UIViewController and Objective-C class

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

I have a Class:

开发者_C百科@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.


-(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.


Probably u.arr returns nil or empty array.

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


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜