2012-01-26 69 views
0

做了一個搜索,但似乎無法找到我正在尋找 基本上我加載到一個nsmutablearray在一個方法中的值,然後我想訪問這些值在另一種方法打印到表 我宣佈在app.h陣列Xcode全球nsmutablearray保持價值

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?某種全局變量?

任何幫助,將不勝感激

感謝

回答

1

以客戶陣列中的應用委託class.declare的財產,在這樣的方法,下面寫的應用程序委託類所合成。 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"]]; 
      } 
     } 

} 

之後,假設你是否想要在另一個類中使用客戶端數組,請這樣做。

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

非常感謝!gna現在就試試吧 –

+0

這一切都在delegate.m類中嗎?謝謝 –

+0

我不明白 – Tendulkar