David Kaplan
David Kaplan

Reputation: 150

xcode global nsmutablearray that keeps values

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

Upvotes: 0

Views: 976

Answers (1)

Tendulkar
Tendulkar

Reputation: 5540

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

Upvotes: 1

Related Questions