保存值的 xcode 全局 nsmutablearray

发布于 2024-12-29 13:51:58 字数 920 浏览 4 评论 0原文

进行了搜索,但似乎无法准确找到我要找的东西 基本上我用一种方法将值加载到 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"]]; 
           }
      }  
}

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

任何帮助将不胜感激

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

与之呼应 2025-01-05 13:51:58

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

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

YourApplicationDelegate *委托= [UIApplication共享应用]委托];

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

}

之后假设您如果想在另一个类中使用客户端数组,请这样做。

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

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); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文