简体   繁体   English

在UIViewController和Objective-C类之间传递NSMutableArray

[英]passing NSMutableArray between UIViewController and Objective-C class

I am trying to pass a NSMutableArray between UIViewController and a class. 我试图在UIViewController和类之间传递NSMutableArray。

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, 在UIViewCOntroller中的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. 当我尝试获取第一个对象[cor objectAtIndex:0]时,它为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. uClass包含一个在后台连续运行的线程,它是通过单击按钮开始的。 During this process getData is called and arr NSMutableArray is populated. 在此过程中,将调用getData并填充arr NSMutableArray。

Now when i move to MapScreen UIViewController and click on a button, it should get the NSMutableArray arr from uClass. 现在,当我移至MapScreen UIViewController并单击一个按钮时,它应该从uClass获取NSMutableArray arr。 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. 到目前为止,您已经创建了一个新的uClass实例,存储在u中。 You initialize it but you do not tell us what happens in init. 您对其进行了初始化,但没有告诉我们init会发生什么。 (in a comment you mentioned that you do not do anything with arr in it.) (在一条评论中,您提到您对arr不做任何事情。)

arr is most probably nil (!) arr最有可能是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指向一个初始化为10的空数组。为什么?

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. 在这里,您将丢弃存储在cor中的空数组和初始化后的数组,并让cor指向insted的内容。 But arr probably points to nowhere. 但是arr可能指向无处。

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. 在下一步中,用arr的内容覆盖cor时,在cor中分配和初始化数组毫无意义。

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 另一个潜在的错误是您没有在类uClass的init方法中给arr分配任何东西,或者您可能想用替换cor的初始化。

[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. 编辑:对不起,我刚刚对上面的问题进行了评论,在这里您告诉getDate最初在后台被调用。

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. 如果将cor分配给arr以便两个变量都指向同一数组对象,则不得将这些变量与任何其他(新创建的)对象关联。 Because if you do so then you loose the link in between. 因为如果这样做,则您之间的链接会丢失。

Please take this suggestion for getData: 请对getData采取以下建议:

-(void)getData{
[arr removeAllObjects];

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

However, arr still needs to be allocated and initilized once. 但是,arr仍然需要分配和初始化一次。 The init method of uClass may be a good place for that. uClass的init方法可能是个好地方。

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

Your new getPosition would simply be: 您的新getPosition将会是:

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

because there is no need to initialize cor. 因为不需要初始化cor。 Just assign the (initialized) value of arr. 只需分配arr的(初始化)值即可。

Probably u.arr returns nil or empty array. 可能u.arr返回nil或空数组。

Also [[NSMutableArray alloc]initWithCapacity 10]; 另外[[NSMutableArray alloc]initWithCapacity 10]; returns empty array. 返回空数组。

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/ 仅针对正在寻找该问题解决方案的任何人,我使用Singleton类来解决此问题,此示例可参考: http : //www.galloway.me.uk/tutorials/singleton-classes/

BR, BR

Suppi 速比

Maybe you could try to override the arr getter to return the pure mutable array : 也许您可以尝试重写arr getter以返回纯可变数组:

- (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. 因为您使用合成的吸气剂获得了NSArray,并且可能没有获得指向原始数组的指针,而是获得了指向其副本的指针,所以taht在您请求它时就为空。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM