xcode保存值的全局nsmutalearray



进行了搜索,但似乎找不到我想要的东西基本上,我在一个方法中将值加载到nsmutalearray中,然后我想在另一个方法中访问这些值,将它们打印到表中我在app.h 中声明了数组

NSMutableArray *clients;

然后在应用程序.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"]]; 
           }
      }  
}

现在我尝试用另一种方法访问客户端数组我看到了一些在应用程序中使用extern的建议。h?某种全局变量?

如有任何帮助,将不胜感激

感谢

在应用程序委托类中获取客户端数组。声明属性,在应用程序代理类中合成。然后在下面的方法中这样写。

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

相关内容

  • 没有找到相关文章