Reputation: 413
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];
}
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;
The above doesn't seem to work when implemented in ClassB.
ClassB *cB = [[ClassB alloc] init];
[cB setArrayReceived:arrayToPass];
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];
Please help!! Thanks!
Upvotes: 1
Views: 2513
Reputation: 230
Take an array in app delegate.Declare the Property and synthesize in the app delegate.And then when you are in the class A,then do like this
Write this code in the class A
Appdelegate *app = (AppDelegate *) [[UIApplication sharedApplication] delegate];
app.somearray = self.yourarray;
In classB you can do like this.
Appdelegate *app =(AppDelegate *) [[UIApplication sharedApplication] delegate];
NSLog(@"arr is %@",app.somearray);
Upvotes: 0
Reputation: 436
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.
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
then make ClassB
a delegate of this
@interface ClassB : <ClassADelegate> { ...
and of course implement the method classADidReceiveData:
in ClassB:
-(void)classADidReceiveData:(NSArray*) array{
arrayReceived = array;
}
Then modify fetchData:
like:
- (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]
}
}
So that when ClassA
received data, it can make ClassB
noticed about that.
The overall usage is:
ClassA *cA = [[ClassA alloc]init];
cA.delegate = self;
cA.view;
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
}
Upvotes: 2
Reputation: 2481
First, you have to have the array in class A be declared as a member variable. And class A needs a method that returns it (could use @synthesize if it is an @property).
Now, you have choices. Class B could have an A instance as a member: then it just asks A for the array (pointer). Class B could be instantiated by someone who passes the array to both A and B. Or the method in A that needs access to B's array could be called by someone who has B as a member and therefore has access to B's array.
There may be more choices yet.
Upvotes: 0
Reputation: 11145
Make the array of class A as a property
of class A then you can easily access it in class B.
in Class A.h
@property (strong, nonatomic) NSMutableArray *tempArray;
in Class A.m
@synthesize tempArray;
then get the instance of class A in class B and you can access tempArray
as class_a_object.tempArray
Upvotes: 0