使用 Xcode 分析器分析死代码

发布于 2024-11-28 05:54:56 字数 5203 浏览 4 评论 0原文

我想分析我的项目,xcode 分析器在我的根控制器中发现一些死代码 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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

油饼 2024-12-05 05:54:56

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

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

因此,像 i + 1i % 2 这样的东西可能只是常量,因为 i 将始终为 0

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.

梦纸 2024-12-05 05:54:56
for (NSInteger i = 0; i < 1; i++)

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

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

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

獨角戲 2024-12-05 05:54:56

你有几个这样的循环:

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

不过,该代码几乎毫无意义...循环体中的代码只会执行一次,而 i == 0,所以你不妨将其从循环。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文