1

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!

1
  • have you tried with a singleton form class A? Commented Apr 5, 2012 at 11:03

4 Answers 4

2

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
}
Sign up to request clarification or add additional context in comments.

7 Comments

Im having a expected identifier error at this line. on class A.h -(void) classADidReceiveData:(NSArray *array);
I'm also having an unknown type name at @property (nonatomic, assign) ClassADelegate *delegate;
Sorry, edited the answer. For expected identifier, change protocol 'ClassADelegate' to protocol 'ClassADelegate <NSObject>' and for unknown type name, change property (nonatomic, assign) 'ClassADelegate *delegate;' to property (nonatomic, assign) 'id<ClassADelegate> delegate;'. I'm now working on android and confused with Java interface :-/
Thanks! @protocol ClassADelegate <NSObject> -(void) classADidReceiveData:(NSArray* array); @end still have the expected identifier error. Thanks! Anyway im form android coming to xcode. Usage is so different.
It's nice to here it helps. Anyway, I'm sorry for the expected identifier error. cA.delegate = self; assigns a ClassB instance to the ClassA's delegate property, so that in fetchedData: ClassA's instance can call ClassB's classADidRecieveData: impelmentation.
|
0

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

5 Comments

ClassA *cA = [[ClassA alloc]init]; ClassA.view; arrayReceived = ClassA.arrayToPass; Isn't this what i have done?
if you alloc class A in class b then you will always get an empty array, you need to get the same instance of class a that is currently in memory.
oh.. so how do i get the same instance?
tell me how r u going from class A to B
I only link them at the storyboards. When i go to next view. Thats who it's called
0

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.

Comments

0

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);

1 Comment

Still cannot seem to get it work... My classA is providing the array, thus im calling my ClassA by ClassA *cA = [[ClassA alloc] init]; cA.view; is this correct?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.