保持值的xcode全局nsmutablearray

时间:2012-01-26 09:02:25

标签: xcode nsmutablearray global-variables

搜索但似乎无法找到我正在寻找的内容 基本上我在一个方法中将值加载到nsmutablearray中然后我想在另一个方法中访问这些值以将它们打印到表中 我在app.h中声明了数组

NSMutableArray *clients;

然后在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"]]; 
           }
      }  
}

现在我尝试以另一种方法访问clients数组 我在app.h中看到了一些使用extern的建议?某种全局变量?

任何帮助将不胜感激

由于

1 个答案:

答案 0 :(得分:1)

将app中的clients数组委托给class.declare属性,在app委托类中合成。然后在下面的方法中写这样的。

- (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"]]; 
           }
      }

}

之后假设你想在另一个类中使用clients数组就是这样。

YourApplicationDelegate *delegate = [UIApplication sharedApplication]delegate]; 
NSLog(@"client array is %@",delegate.clients);