2011-09-17 23 views
1

出於某種原因,這個代碼不工作:目標C保留更改由塊製成的產權

[request setCompletionBlock:^{ 
    NSString *response = [request responseString]; 
    NSDictionary *data = [response JSONValue]; 
    NSArray *events = (NSArray *)[data objectForKey:@"objects"]; 

    for (NSMutableDictionary *event in events){ 

     /* experimental code*/ 
     NSString *urlString = [NSString 
           stringWithFormat: 
           @"http://localhost:8000%@?format=json", 
           [event objectForKey:@"tournament"]]; 

     NSURL *url2 = [NSURL URLWithString:urlString]; 
     ASIHTTPRequest *request2 = [ASIHTTPRequest requestWithURL:url2]; 
     [request2 setCompletionBlock:^{ 
      NSString *responseString = [request2 responseString]; 
      NSDictionary *tournamentDict = [responseString JSONValue]; 
      self.tournamentString = [tournamentDict objectForKey:@"tournament"]; 
     }]; 
     [request2 startAsynchronous]; 

     /* end experimental code */ 
     NSLog(@"%@", self.tournamentString); 
     [mutableArray addObject:event]; 
    } 
    self.eventsArray = mutableArray; 
    [MBProgressHUD hideHUDForView:self.view animated:YES]; 

    [self.tableView reloadData]; 
}]; 

所以這裏有2個異步請求,在我火了一個又一個。我想在第二個請求執行後更改屬性tournamentText的值。

在第二個請求的完成塊內部,當我NSLog self.tournamentText時,它顯示我想要檢索的文本。

在塊之外,NSLog產生一個零。

我該怎麼做才能保留對self.tournamentText的更改?先謝謝了!請告訴我,如果我錯過了關於此的Apple文檔。

+0

Outsideblock產生nil的原因是request2異步,它在request2完成之前執行。 – Saran

回答

1

您應該將__block存儲類型修飾符應用於變量(塊外部)。

__block NSDictionary * tournamentDict;

有關更多信息,請參閱Apple的documentation on interaction between blocks and variables(在Blocks Programming Topics中)。

順便說一下,你確實意識到你在一個塊內有一個塊,而不是兩個獨立的塊在另一個之內?要保留對第二個塊之外的變量的更改,請嘗試以下操作:

[request setCompletionBlock:^{ 
    NSString *response = [request responseString]; 
    NSDictionary *data = [response JSONValue]; 
    NSArray *events = (NSArray *)[data objectForKey:@"objects"]; 

    for (NSMutableDictionary *event in events){ 

     /* experimental code*/ 
     NSString *urlString = [NSString 
           stringWithFormat: 
           @"http://localhost:8000%@?format=json", 
           [event objectForKey:@"tournament"]]; 

     NSURL *url2 = [NSURL URLWithString:urlString]; 
     ASIHTTPRequest *request2 = [ASIHTTPRequest requestWithURL:url2]; 
     __block NSDictionary *tournamentDict; 
     [request2 setCompletionBlock:^{ 
      NSString *responseString = [request2 responseString]; 
      tournamentDict = [responseString JSONValue]; 
      self.tournamentString = [tournamentDict objectForKey:@"tournament"]; 
     }]; 
     [request2 startAsynchronous]; 

     /* end experimental code */ 
     NSLog(@"%@", self.tournamentString); 
     [mutableArray addObject:event]; 
    } 
    self.eventsArray = mutableArray; 
    [MBProgressHUD hideHUDForView:self.view animated:YES]; 

    [self.tableView reloadData]; 
}]; 
+0

是的,有2個街區。我有兩個請求和每個請求的setCompletionBlock:方法。就像我剛剛說的那樣,第一個請求在第二個請求後觸發 – yretuta

+0

的確,我只是想確保你知道它們不是完全獨立的塊。你也可以把「__block NSDictionary * tournamentDict;」在第一個塊之前,我不確定這是如何影響範圍的。如上所述添加__block是否適合您?它確實是 – Sjoerd

+0

。我已經接受了答案:) – yretuta