奇怪的开关大小写错误(iPhone sdk)UITableView

问题描述:

我得到一个奇怪的编译错误 - 我不知道是否有一个“机器中的鬼”或什么?奇怪的开关大小写错误(iPhone sdk)UITableView

下面如果代码段我收到此错误

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

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



cell.selectionStyle = UITableViewCellSelectionStyleNone; 

if (indexPath.section == 0) { 

    switch (indexPath.row) { 
    case 0: 
    cell.textLabel.text = @"User"; 
    cell.detailTextLabel.text = @"John Smith"; 
    break; 
    case 1: 
    cell.textLabel.text = @"From"; 
    cell.detailTextLabel.text = @"12/01/2010"; 
    break; 
    case 2: 
    cell.textLabel.text = @"To"; 
    cell.detailTextLabel.text = @"12/02/2010"; 
    break; 
    case 3: 
    cell.textLabel.text = @"Duration"; 
    cell.detailTextLabel.text = @"30 Days"; 
    break; 
    case 4: 
    UITextView *commentsView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, 300, 44)]; 
    //commentsView.layer.cornerRadius = 10.0f; 
    //[cell.contentView addSubview:commentsView]; 
    //[commentsView release]; 
    break; 
    default: 
    break; 
    } 
    } else if (indexPath.section == 1) { 
    cell.detailTextLabel.text = @"Approve"; 
} else { 
    cell.detailTextLabel.text = @"Reject"; 
} 
    // Configure the cell... 

    return cell; 
} 

我得到的情况下,4在行

UITextView *commentsView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, 300, 44)]; 

以下错误

/Users/dk/Desktop/xxx/Classes/DetailedLeaveRequestViewController.m:103:0 
/Users/dk/Desktop/xxx/Classes/DetailedLeaveRequestViewController.m:103: error: expected expression before 'UITextView' 

I double-chec一遍又一遍地翻译语法和一切,但是找不到任何问题。 奇怪的部分是当我试图编译案例3的最后一行下面的行时:(正好在break;上面)编译并正确运行。 我们不能在case语句中声明和初始化一个对象吗? 我在这里错过了相当基本的东西吗?

谢谢。

+0

我觉得这是愚弄的人:http://*.com/问题/ 92396/why-cant-variables-be-decla-in-a-switch-statement和http://*.com/questions/3757445/switch-case-declaration-with-initialization-declaration-and-then-分配 - 参考这些,因为他们有详细的解释。 – 2010-10-11 10:18:28

你需要一个块限制器添加到您的case语句:

case 4:{ 
    UITextView *commentsView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, 300, 44)]; 
} 

或移动switch语句之外的变量声明

+0

这是出于特定原因吗?以上所有情况都可以通过多个没有块分隔符的语句正确编译。当我将代码粘贴到cell.detailTextLabel.text = @“30 Days”下面时,在情况3中:没有分隔符,它编译并运行良好。请告诉我。谢谢。 – Dev 2010-10-11 10:18:22

+0

@neo,当时正在寻找一个链接SedateAlien发布了他们 - 你可以找到很好的解释 – Vladimir 2010-10-11 10:22:07

+0

@neo:参考[this](http://*.com/questions/1180550/weird-switch-error-in- obj -c/1181106#1181106)在另一个SO线程中的特定答案。它详细解释。 :-) – 2010-10-11 10:24:04