ブロック文についての質問です。私の質問は、ブロック文の変数を取り出す方法です。私のコードを見てください。以下のコードはうまく動作しません。
__block NSURL *ssURL
AFJSONRequestOperation *operation =
[AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
ssURL = [NSURL URLWithString:[JSON valueForKeyPath:@"screenshot"]];
[self currentEntry].ogImageURL = ssURL;
NSLog(@"%@", ssURL); // -> "http://correct.address"
} failure:nil];
[operation start];
また、変数 ogImageURL を以下のように設定します。
@property (copy, atomic) NSURL *ogImageURL;
完了ブロックのうち、NSLog には明らかに「(null)」が表示されます。ブロック文の外で変数を使いたい。変ですね。
セルが呼び出して配列に自己情報を取得するテーブルビューを作成したいので、ブロックの外側から変数を取得する必要があります。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = nil;
if (indexPath.row < self.itemsArray.count) {
cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
UIImageView *imageViewOgImage = (UIImageView *)[cell viewWithTag:5];
NSURL *ogImageURL = [[self.itemsArray objectAtIndex:indexPath.row] ssURL];
完全なソースは、この 186 行目です: https://github.com/weed/p120711_TechMovie/blob/120723/TechMovie/RSSParser.m