X code来自不同类别传递的NSMutableArray [英] XCode Passing NSMutableArray from Different Classes

查看:113
本文介绍了X code来自不同类别传递的NSMutableArray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个班。 A和B的ClassA里面我有检索JSON数据,并添加到一个数组的方法。我想访问从ClassB的数组。我怎样才能实现呢?

I have 2 classes. A and B. Inside ClassA I have a method which retrieve JSON data and adds into an array. I want to access this array from ClassB. How can I achieve it?

ClassA.h
- (void)viewDidLoad
{
//initialise arrayPlaces and arrayWeather
[super viewDidLoad];

dispatch_async(queue, ^{
NSData* data = [NSData dataWithContentsOfURL: 
                    serverURL];
                    [self performSelectorOnMainThread:@selector(fetchedData:) 
                    withObject:data waitUntilDone:YES];
});
}

- (void)fetchedData:(NSData *)responseData {

//parse out the json data
NSError *error;
NSMutableArray *tempArray = [[NSMutableArray alloc] init];
tempArray = [NSJSONSerialization 
             JSONObjectWithData:responseData //1
             options:kNilOptions 
             error:&error];

//declare arrayPlaces
arrayToPass = [[NSMutableArray alloc] init];

//...codes to add array here using a loop...
[arrayToPass addObject:tempString];
}

在ClassB的,我有一个的tableView,我想所有的ClassA从数组。我怎样才能做到这一点?

In ClassB, I have a tableView which i want to get all the array from ClassA. How can I achieve this?

ClassA *cA = [[ClassA alloc]init];
ClassA.view;
arrayReceived = ClassA.arrayToPass;

以上似乎并不在ClassB的实现时工作。

The above doesn't seem to work when implemented in ClassB.

ClassB *cB = [[ClassB alloc] init];
[cB setArrayReceived:arrayToPass];

在code的这一部分后,ClassA的实施时

无论做这项工作。
//...$c$cs在这里添加阵列使用一个循环?
[arrayToPass ADDOBJECT:tempString];

Neither does this work when implemented in ClassA after this portion of the code. "//...codes to add array here using a loop... [arrayToPass addObject:tempString];

请帮助!谢谢!

推荐答案

您code不工作的原因是因为 fetchData:是异步完成和 arrayToPass 仍然没有填充当code arrayReceived = ClassA.arrayToPass; 运行

The reason your code does not work is because fetchData: is done asynchronously and arrayToPass is still not populated when the code arrayReceived = ClassA.arrayToPass; runs.

因此​​,我们必须让 ClassB的知道。
如何第一 ClassA的 ClassB的之间声明一个协议 ClassADelegate ,并在 ClassA的声明一个属性该类型的 ClassADelegate 是这样的:

So we have to let ClassB know about that. How about first declaring a protocol ClassADelegate between ClassA and ClassB, and declare a property in ClassA that of type ClassADelegate like this:

in ClassA.h
@protocol ClassADelegate ;//Forward declaration of the protocol
@interface ClassA {...
}
@property (nonatomic, assign) id <ClassADelegate> delegate;
@end

@protocol ClassADelegate <NSObject> 
-(void) classADidReceiveData:(NSArray *)array;
@end

然后让 ClassB的的这种委托

@interface ClassB : <ClassADelegate> { ...

和课程实施方法 classADidReceiveData:在ClassB的

-(void)classADidReceiveData:(NSArray*) array{
 arrayReceived = array;
}

然后修改 fetchData:这样的:

- (void)fetchedData:(NSData *)responseData {

...
//...codes to add array here using a loop...
[arrayToPass addObject:tempString];

 //Call the delegate method if a delegate has been set
if(delegate){
     [delegate classADidReceiveData:arrayToPass]
}
}

这样当 ClassA的接收到的数据,它可以使 ClassB的注意到这一点。

So that when ClassA received data, it can make ClassB noticed about that.

整体的用法是:

ClassA *cA = [[ClassA alloc]init];
cA.delegate = self;
cA.view;

如果您在 ClassB的获取数据后要做什么,然后做在 classADidReceiveData ClassB的

If you have something to be done after fetching data in ClassB, then do it in classADidReceiveData in ClassB.

-(void)classADidReceiveData:(NSArray *)array{
 arrayReceived = array;
 //Do whatever you want after receiving array
}

这篇关于X code来自不同类别传递的NSMutableArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆