使用Xcode分析器的死代码

时间:2011-08-05 13:05:06

标签: ios xcode dead-code

我想分析我的projet,xcode分析器在我的rootcontroller中找到一些死代码 Xcode告诉我:

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

有人可以解释一下如何清理代码,谢谢......

这是我的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

3 个答案:

答案 0 :(得分:6)

createRows方法中,您有一个只执行一次的循环:

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

因此,i + 1i % 2之类的内容可能只是常量,因为i始终为0

答案 1 :(得分:1)

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

你的for循环从0开始,结束于0. i ++永远不会被执行。

答案 2 :(得分:1)

你有几个形式的循环:

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

这段代码几乎毫无意义,但是......循环体中的代码只执行一次,而i == 0,所以你不妨把它从循环中取出来。