284

将项目转换为使用 ARC 时,“switch case 在受保护范围内”是什么意思?我正在使用 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

用大括号围绕每个案例本身{}。那应该可以解决问题(在我的一个项目中对我有用)。

于 2011-09-26T23:40:47.213 回答
14

不看代码很难确定,但这可能意味着开关内部正在进行一些变量声明,编译器无法判断是否有通往所需释放点的清晰路径。

于 2011-09-26T22:51:54.183 回答
9

有两种简单的方法可以解决这个问题:

  • 您可能正在声明变量。将变量的声明移到 switch 语句之外
  • 将整个 case 块放在大括号 {} 之间

当要释放变量时,编译器无法计算代码行。导致这个错误。

于 2014-12-18T07:36:02.667 回答
5

对我来说,问题从开关中间开始,大括号没有解决,除非您必须在所有以前的案例语句中包含 {}。对我来说,当我收到声明时,错误就来了

NSDate *start = [NSDate date];

在前一种情况下。在我删除它之后,所有后续的 case 语句都从受保护范围错误消息中清除了

于 2012-12-05T06:36:53.127 回答
3

前:

    case 2:
        NSDate *from = [NSDate dateWithTimeIntervalSince1970:1388552400];
        [self refreshContents:from toDate:[NSDate date]];
        break;

我在切换之前移动了 NSDate 定义,它修复了编译问题:

NSDate *from;  /* <----------- */
switch (index) {
    ....
    case 2:
        from = [NSDate dateWithTimeIntervalSince1970:1388552400];
        [self refreshContents:from toDate:[NSDate date]];
        break;

}
于 2014-02-23T20:37:30.453 回答
2

在 switch 外部声明变量,然后在 case 内部实例化它们。这对我使用 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语句和break{}之间的代码括起来。它适用于我的代码。

于 2015-08-12T01:19:43.947 回答