0

Did a search but cant seem to find exactly what I'm looking for Basically I load values into a nsmutablearray in one method and then I want to access these values in another method to print them to a table I declared the array in the app.h

NSMutableArray *clients;

Then in the app.m

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
      [connection release];
      NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
      [responseData release];
      NSArray *results = [responseString JSONValue];
      clients = [[NSMutableArray alloc]init];

      // Loop through each entry and add clients to array
      for (NSDictionary *entry in results)
      {
           if (![clients containsObject:[entry objectForKey:@"client"]]) 
           {   
                [clients addObject:[entry objectForKey:@"client"]]; 
           }
      }  
}

Now Im try to acces the clients array in another method I have seen some suggestions to use extern in the app.h? Some sort of global variable?

Any help would be appreciated

Thanks

1 Answer 1

1

Take the clients array in app delegate class.declare the property,synthesizes in the app delegate class.Then in the below method write like this.

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
      [connection release];

YourApplicationDelegate *delegate = [UIApplication sharedApplication]delegate];

  NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
  [responseData release];
  NSArray *results = [responseString JSONValue];

      clients = [[NSMutableArray alloc]init];

      // Loop through each entry and add clients to array
      for (NSDictionary *entry in results)
      {
           if (![clients containsObject:[entry objectForKey:@"client"]]) 
           {   
                [delegate.clients addObject:[entry objectForKey:@"client"]]; 
           }
      }

}

after that suppose you if you want to use the clients array in another class do like this.

YourApplicationDelegate *delegate = [UIApplication sharedApplication]delegate]; 
NSLog(@"client array is %@",delegate.clients); 
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks alot!gna give it a try now
Is this all in the delegate.m class? Thanks
this whole connectionDId finish loading method? is this in another class or in the delegate class?
Write the above did finishing method ,in app.m

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.