使用 Xcode 分析器分析死代码
我想分析我的项目,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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在
createRows
方法中,您有一个只会执行一次的循环:因此,像
i + 1
和i % 2
这样的东西可能只是常量,因为i
将始终为0
。In your
createRows
method, you have a loop that will only execute once:Therefore, things like
i + 1
andi % 2
could just be constants sincei
will always be0
.你的 for 循环从 0 开始,到 0 结束。 i++ 永远不会被执行。
Your for loop starts at 0, ends at 0. i++ will never be executed.
你有几个这样的循环:
不过,该代码几乎毫无意义...循环体中的代码只会执行一次,而 i == 0,所以你不妨将其从循环。
You've got several loops of the form:
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.