When converting a project to use ARC what does “sw

2019-01-07 03:26发布

When converting a project to use ARC what does "switch case is in protected scope" mean? 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.

Edit, Here is the code:

the ERROR is marked on the "default" case:

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

8条回答
祖国的老花朵
2楼-- · 2019-01-07 04:08

Declare the variables outside the switch, then instantiate them inside the case. That worked perfectly for me using Xcode 6.2

查看更多
爷的心禁止访问
3楼-- · 2019-01-07 04:12

There are 2 easy ways to solve this issue:

  • You are probably declaring variables. Move the declaration of the variables outside the switch statement
  • 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.

查看更多
贼婆χ
4楼-- · 2019-01-07 04:15

Surround each case itself with braces {}. That should fix the issue (it did for me in one of my projects).

查看更多
戒情不戒烟
5楼-- · 2019-01-07 04:19

Surround with braces {} the code between the case statement and the break in each case. It worked on my code.

查看更多
够拽才男人
6楼-- · 2019-01-07 04:20

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

}
查看更多
叛逆
7楼-- · 2019-01-07 04:23

Hard to be sure without looking at the code, but it probably means there's some variable declaration going on inside the switch and the compiler can't tell if there's a clear path to the required dealloc point.

查看更多
登录 后发表回答