-1

私の-viewDidLoadメソッドでは、多数の を初期化し、それらをクラス ヘッダー ファイル内の初期化された宣言された viaにNSMutableDictionaries追加します。関連するコードを以下に示します。要するに、私は HTML Web ページから情報を Web スクレイピングしています。NSMutableArray@property

 NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
_regionalDicts = [[NSMutableArray alloc] init];

for (int i = 0; i < [strings count]; i++) {
    NSString *str = [strings objectAtIndex:i];
    //Property parser:
    if ([str rangeOfString:@"<td>"].location != NSNotFound) {
        NSString *parsedTD1 = [str stringByReplacingOccurrencesOfString:@"<td>" withString:@""];
        NSString *parsedTD2 = [parsedTD1 stringByReplacingOccurrencesOfString:@"</td>" withString:@""];
        NSString *parsedTD3 = [parsedTD2 stringByReplacingOccurrencesOfString:@"&nbsp;" withString:@"\n"];


        NSString *final = [parsedTD3 stringByReplacingOccurrencesOfString:@"\t" withString:@""];

        //NSLog(@"Final string: %@", final);

        if ([final isEqualToString:@""]) {
            continue;
        }


        if (gotEventType == NO) {
            gotEventType = YES;
            [dict setObject:final forKey:@"type"];
            continue;
        }

        if (gotRegional == YES && gotLocation == NO) {
            gotLocation = YES;
            [dict setObject:final forKey:@"location"];
            continue;
        }
        if (gotLocation == YES && gotCity == NO) {
            gotCity = YES;
            NSString *cityToReturn = [final stringByReplacingOccurrencesOfString:@"\n" withString:@""];
            [dict setObject:cityToReturn forKey:@"city"];
            continue;
        }     
        if (gotRegional == YES && gotEventType == YES && gotCity == YES && gotLocation == YES && gotURL == YES) {
            gotRegional = NO;
            gotEventType = NO;
            gotCity = NO;
            gotLocation = NO;
            gotURL = NO;

            NSLog(@"Regional: %@", [dict objectForKey:@"regional"]);
            NSLog(@"Type: %@", [dict objectForKey:@"type"]);
            NSLog(@"City: %@", [dict objectForKey:@"city"]);

            //Testing to see if anything is nil
            NSLog(@"Location: %@\n", [dict objectForKey:@"location"]);
            if (!_regionalDicts) {
                NSLog(@"Dict is nil");
            }
            [_regionalDicts addObject:dict];
            NSLog(@"Objects in array: %u", [_regionalDicts count]);
            NSMutableDictionary *tempDict = [_regionalDicts objectAtIndex:[_regionalDicts count]-1];
            NSLog(@"Regional in array: %@", [tempDict objectForKey:@"regional"]);
            [dict removeAllObjects];

            continue;
        }

生成された辞書が生成され、_regionalDicts変更可能な配列内に保持されることは明らかです。これは、次のようにヘッダー ファイルで宣言されます。

@property (strong, nonatomic) IBOutlet NSMutableArray *regionalDicts;

ただし、同じクラスのテーブル ビュー セルに情報を渡そうとすると、辞書の内容が null になります。私が期待している辞書と同じ数のオブジェクトが配列内にありますが、それらにはコンテンツが含まれていません。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    if (_regionalDicts) {
        NSMutableDictionary *dict = [_regionalDicts objectAtIndex:0];
        NSLog(@"Setting label %@", [dict objectForKey:@"city"]);
        [cell.textLabel setText:[dict objectForKey:@"regional"]];
    }

    return cell;
}

戻り値:

2013-04-01 19:58:50.250 MatchScrape[53570:207] Setting label (null)

メモリ管理の問題が原因だとしか思えません。クラス配列が追加されたメソッドのスコープ外にアクセスすると、クラス配列の内容が無効になるのに、配列が同じカウントを保持できるのはなぜですか?

4

1 に答える 1