1

I am getting a weird compilation error - I dont know if there is a "ghost in the machine" or what?

Below if the code snippet where I am getting this error

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

I am getting following error

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

at line

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

in the case 4:

I double-checked the syntax and everything over and over but could not find any problems. The weird part is when I tried to compile the line below last line of case 3: (just above the break;) it compiled and ran correctly. Can we not declare and initialize an object inside a case statement? Am I missing something fairly basic here?

Thanks.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
Dev
  • 6,207
  • 4
  • 27
  • 32
  • I think this is a dupe of: http://stackoverflow.com/questions/92396/why-cant-variables-be-declared-in-a-switch-statement and http://stackoverflow.com/questions/3757445/switch-case-declaration-with-initialization-declaration-and-then-assignment -- refer to these as they have detailed explanations. – Aidan Steele Oct 11 '10 at 10:18

1 Answers1

2

You need to add a block limiter to your case statement:

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

or move variable declaration outside of switch statement

Vladimir
  • 170,431
  • 36
  • 387
  • 313
  • Is that for specific reason? All the above cases are getting compiled correctly with multiple statements without block delimiters. Also when I pasted the code below line cell.detailTextLabel.text = @"30 Days"; in case 3: without delimiters, it compiled and ran fine. Please let me know. Thanks. – Dev Oct 11 '10 at 10:18
  • @neo, while was looking for a link SedateAlien posted them - you can find good explanation there – Vladimir Oct 11 '10 at 10:22
  • @neo: Refer to [this](http://stackoverflow.com/questions/1180550/weird-switch-error-in-obj-c/1181106#1181106) specific answer in another SO thread. It explains in great detail. :-) – Aidan Steele Oct 11 '10 at 10:24