0

i wanted to analyse my projet, and xcode analyser find some Dead Code in my rootcontroller Xcode tell me that :

Dead code
The left operand to '+' is always 0
-> Variable 'i' initialized to 0
-> The left operand to '+' is always 0

Can someone explain me this ans how to clean that code, thanks....

here is my rootcontroller.m

#import "RootViewController22.h"
#import "LabelCell.h"
#import "NibLoadedCell.h"
#import "GradientBackgroundTable.h"
#import "NibLoadedCell2.h"
#import "NibLoadedCell3.h"

@implementation RootViewController22

//
// title
//
// returns the navigation bar text for the front screen
//


/*- (NSString *)title
{
    return NSLocalizedString(@"Les Robes du Bengal", @"");
}*/



//
// createRows
//
// Constructs all the rows on the front screen and animates them in
//
- (void)createRows
{


    [self addSectionAtIndex:0 withAnimation:UITableViewRowAnimationFade];
    for (NSInteger i = 0; i < 1; i++)
    {
        [self
         appendRowToSection:0
         cellClass:[NibLoadedCell2 class]
         cellData:[NSString stringWithFormat:
                   NSLocalizedString(@"This is row %ld", @""), i + 1]
         withAnimation:(i % 2) == 0 ?
         UITableViewRowAnimationLeft :
         UITableViewRowAnimationRight];
    }




    //[self addSectionAtIndex:0 withAnimation:UITableViewRowAnimationFade];
    //for (NSInteger i = 0; i < 4; i++)
    //{
    //  [self
    //      appendRowToSection:0
    //      cellClass:[LabelCell class]
    //      cellData:[NSString stringWithFormat:
    //          NSLocalizedString(@"This is row %ld", @""), i + 1]
    //      withAnimation:(i % 2) == 0 ?
    //          UITableViewRowAnimationLeft :
    //          UITableViewRowAnimationRight];
//  }

    [self addSectionAtIndex:1 withAnimation:UITableViewRowAnimationFade];
    for (NSInteger i = 0; i < 1; i++)
    {
        [self
            appendRowToSection:1
            cellClass:[NibLoadedCell3 class]
            cellData:[NSString stringWithFormat:
                NSLocalizedString(@"This is row %ld", @""), i + 1]
            withAnimation:(i % 2) == 0 ?
                UITableViewRowAnimationLeft :
                UITableViewRowAnimationRight];
    }

//  [self addSectionAtIndex:2 withAnimation:UITableViewRowAnimationFade];
//  for (NSInteger i = 0; i < 4; i++)
//  {
//      [self
//          appendRowToSection:2
    //      cellClass:[TextFieldCell class]
    //      cellData:
    //          [NSMutableDictionary dictionaryWithObjectsAndKeys:
    //              [NSString stringWithFormat:
    //                  NSLocalizedString(@"TextField %ld", @""), i + 1],
    //                  @"label",
    //              @"", @"value",
    //              NSLocalizedString(@"Value goes here", @""),
    //                  @"placeholder",
    //          nil]
        //  withAnimation:(i % 2) == 0 ?
    //          UITableViewRowAnimationLeft :
    //          UITableViewRowAnimationRight];
//  }

    [self addSectionAtIndex:2 withAnimation:UITableViewRowAnimationFade];
    for (NSInteger i = 0; i < 1; i++)
    {
        [self
         appendRowToSection:2
         cellClass:[NibLoadedCell class]
         cellData:[NSString stringWithFormat:
                   NSLocalizedString(@"This is row %ld", @""), i + 1]
         withAnimation:(i % 2) == 0 ?
         UITableViewRowAnimationLeft :
         UITableViewRowAnimationRight];
    }


    [self hideLoadingIndicator];
}

//
// refresh
//
// Removes all existing rows and starts a reload (on a 0.5 second timer)
//
- (void)refresh:(id)sender
{
    [self removeAllSectionsWithAnimation:UITableViewRowAnimationFade];
    [self performSelector:@selector(createRows) withObject:nil afterDelay:0.5];
    [self showLoadingIndicator];
}

//
// viewDidLoad
//
// On load, refreshes the view (to load the rows)
//
- (void)viewDidLoad
{
    self.navigationController.navigationBar.tintColor = [UIColor blackColor];
     self.title = @"Les Robes";
    [super viewDidLoad];

    self.useCustomHeaders = YES;
    [self refresh:nil];
}

//
// loadView
//
// Since the view is so simple (just a GradientBackgroundView) we might as
// well contruct it in code.
//
- (void)loadView
{
    GradientBackgroundTable *aTableView =
        [[[GradientBackgroundTable alloc]
            initWithFrame:CGRectZero
            style:UITableViewStyleGrouped]
        autorelease];

    self.view = aTableView;
    self.tableView = aTableView;
}

//
// textFieldDidEndEditing:
//
// Update the rowData for the text field rows to match the edited value of the
// text field.
//

//
// tableView:titleForHeaderInSection:
//
// Header text for the three sections
//
// Parameters:
//    aTableView - the table
//    section - the section for which header text should be returned
//
// returns the header text for the appropriate section
//
- (NSString *)tableView:(UITableView *)aTableView
    titleForHeaderInSection:(NSInteger)section
{
    if (section == 0)
    {
        return NSLocalizedString(@"Les Motifs", nil);
    }
    else if (section == 1)
    {
        return NSLocalizedString(@"Les Couleurs", nil);
    }
    else if (section == 2)
    {
        return NSLocalizedString(@"À Savoir", nil);
    }

    return nil;
}

@end
a3116b
  • 337
  • 1
  • 5
  • 13
  • 5
    Could you post only code you want to fix? It's really hard to go through irrelevant lines to find what exactly you need... – Vladimir Aug 05 '11 at 13:12
  • @Vladimir [self addSectionAtIndex:0 withAnimation:UITableViewRowAnimationFade]; for (NSInteger i = 0; i < 1; i++) { [self appendRowToSection:0 cellClass:[NibLoadedCell2 class] cellData:[NSString stringWithFormat: NSLocalizedString(@"This is row %ld", @""), i + 1] withAnimation:(i % 2) == 0 ? UITableViewRowAnimationLeft : UITableViewRowAnimationRight]; } – a3116b Aug 05 '11 at 16:12

3 Answers3

6

In your createRows method, you have a loop that will only execute once:

for (NSInteger i = 0; i < 1; i++) { ... }

Therefore, things like i + 1 and i % 2 could just be constants since i will always be 0.

Sean Carpenter
  • 7,681
  • 3
  • 37
  • 38
1
for (NSInteger i = 0; i < 1; i++)

Your for loop starts at 0, ends at 0. i++ will never be executed.

progrmr
  • 75,956
  • 16
  • 112
  • 147
1

You've got several loops of the form:

for (NSInteger i = 0; i < 1; i++) { }

That code is nearly meaningless, though... the code in the body of the loop will only ever execute once, while i == 0, so you might as well just take it out of the loop.

Caleb
  • 124,013
  • 19
  • 183
  • 272
  • i tried to change some value, if I change i = 0 to a orner value then the tableview Will the name or the section and 0 cell visible in it. if i change i< 1 to a orner value 0 Will display nothing and orvet than 1 Will display the same cell in my section, so i've many same cells – a3116b Aug 05 '11 at 17:34