将项目转换为使用ARC时,“切换案例是否在受保护的范围内”是什么意思?

本文翻译自:When converting a project to use ARC what does “switch case is in protected scope” mean?

When converting a project to use ARC what does "switch case is in protected scope" mean? 将项目转换为使用ARC时,“切换案例是否在受保护的范围内”是什么意思? I am converting a project to use ARC, using Xcode 4 Edit -> Refactor -> Convert to Objective-C ARC... One of the errors I get is "switch case is in protected scope" on "some" of the switches in a switch case. 我正在转换一个项目使用ARC,使用Xcode 4编辑 - >重构 - >转换为Objective-C ARC ...我得到的一个错误是“切换案例在受保护的范围内”的“部分”交换机开关盒。

Edit, Here is the code: 编辑,这是代码:

the ERROR is marked on the "default" case: ERROR标记在“默认”情况下:

- (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;
}

#1楼

参考:https://stackoom.com/question/vJgX/将项目转换为使用ARC时-切换案例是否在受保护的范围内-是什么意思


#2楼

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

Note: Check! 注意:检查! The syntax of the bold & italicized line. 粗体和斜体线的语法。 Rectify it and you are good to go. 整顿它,你很高兴。


#3楼

For me, the problem started on the middle of a switch and curly brackets did not worked out, unless you have to include {} IN ALL previous case statements. 对我来说,问题始于开关的中间,并且大括号没有解决,除非你必须在所有之前的case语句中包含{}。 For me the error came when I had the statement 对我来说,错误发生在我发表声明时

NSDate *start = [NSDate date];

in the previous case. 在前一种情况下。 After I deleted this, then all subsequent case statement came clean from the protected scope error message 删除之后,所有后续的case语句都从受保护的范围错误消息中清除


#4楼

Before: 之前:

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

I moved NSDate definition before switch, and it fixed the compile problem: 我在切换之前移动了NSDate定义,并修复了编译问题:

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

}

#5楼

There are 2 easy ways to solve this issue: 有两种简单的方法可以解决这个问题:

  • You are probably declaring variables. 您可能正在声明变量。 Move the declaration of the variables outside the switch statement 将变量声明移到switch语句之外
  • Put the whole case block in between curly brackets {} 将整个案例块放在大括号之间{}

The compiler can not calculate the code line when the variables are to be released. 当要释放变量时,编译器无法计算代码行。 Causing this error. 导致此错误。


#6楼

Declare the variables outside the switch, then instantiate them inside the case. 在switch之外声明变量,然后在case中实例化它们。 That worked perfectly for me using Xcode 6.2 这对我来说非常适合使用Xcode 6.2

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值