284

プロジェクトを ARC を使用するように変換する場合、「スイッチ ケースは保護された範囲内にあります」とはどういう意味ですか? Xcode 4 Edit -> Refactor -> Convert to Objective-C ARC を使用して、プロジェクトを ARC を使用するように変換しています。スイッチケースです。

編集、コードは次のとおりです。

エラーは「デフォルト」の場合にマークされています。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"";
    UITableViewCell *cell ;
    switch (tableView.tag) {
        case 1:
            CellIdentifier = @"CellAuthor";
            cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
            if (cell == nil) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
        cell.textLabel.text = [[prefQueries objectAtIndex:[indexPath row]] valueForKey:@"queryString"];
        break;
    case 2:
        CellIdentifier = @"CellJournal";
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
        cell.textLabel.text = [[prefJournals objectAtIndex:[indexPath row]] valueForKey:@"name"];

        NSData * icon = [[prefJournals objectAtIndex:[indexPath row]] valueForKey:@"icon"];
        if (!icon) {
            icon = UIImagePNGRepresentation([UIImage imageNamed:@"blank72"]);
        }
        cell.imageView.image = [UIImage imageWithData:icon];

        break;

    default:
        CellIdentifier = @"Cell";
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
            }
        break;
    }


    return cell;
}
4

8 に答える 8

651

各ケース自体を中括弧で囲みます{}。これで問題は解決するはずです(私のプロジェクトの1つで解決しました)。

于 2011-09-26T23:40:47.213 に答える
14

コードを見ずに確認するのは難しいですが、スイッチ内で何らかの変数宣言が行われており、必要な解放ポイントへの明確なパスがあるかどうかをコンパイラが判断できないことを意味している可能性があります。

于 2011-09-26T22:51:54.183 に答える
9

この問題を解決するには、2 つの簡単な方法があります。

  • おそらく変数を宣言しています。変数の宣言を switch ステートメントの外に移動します
  • case ブロック全体を中かっこ {} の間に入れます

コンパイラは、変数が解放されるときにコード行を計算できません。このエラーの原因。

于 2014-12-18T07:36:02.667 に答える
5

私にとって、問題はスイッチの途中で始まり、前のすべてのケースステートメントに {} を含める必要がない限り、中括弧は機能しませんでした。私にとっては、ステートメントを持っていたときにエラーが発生しました

NSDate *start = [NSDate date];

前の場合。これを削除した後、後続のすべてのケースステートメントは、保護されたスコープのエラーメッセージからきれいになりました

于 2012-12-05T06:36:53.127 に答える
2

スイッチの外側で変数を宣言してから、ケース内でインスタンス化します。Xcode 6.2を使用して私にとっては完璧に機能しました

于 2015-03-20T17:18:00.090 に答える
1
default:
        CellIdentifier = @"Cell";
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            ***initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];***
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
            }
        break;
    }

注:チェック!太字と斜体の行の構文。それを修正すれば、準備完了です。

于 2012-08-16T16:40:17.047 に答える
0

各ケースのcaseステートメントとブレーク{}の間のコードを中かっこで囲みます。それは私のコードで動作しました。

于 2015-08-12T01:19:43.947 に答える