Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7757801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:11:16+00:00 2026-06-01T13:11:16+00:00

I have 2 classes. A and B. Inside ClassA I have a method which

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T13:11:17+00:00Added an answer on June 1, 2026 at 1:11 pm

    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
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have some php classes inside a namespace com\test and want to import
I have a abstract generic class. I want to define a method inside there
I have a FUU constante inside Foo and Foo2 classes, and in order to
I have classes which have automatic properties only like public customerName {get; set;}. They
I have classes structured like this: Public MustInherit Class A ' several properties End
I have a set of classes which extend an abstract class. The abstract class
I have 80-90 classes which are all a type of calculation. But each class
I have two closely related classes which I'll call Widget and Sprocket. Sprocket has
I have an Entity baseclass which the classes Player and Enemy Inherit. class Entity
I need to pass different objects instantiated from different classes which they have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.