IOS之XCode静态分析

常见问题缺陷与解决办法

1.Returning 'self' while it is not set to the result of '[(super or self) init...] 

self 未被赋值 就被返回;

错误:-(instancetype)init{

if(self ==[super init])

{

}

return self;

}

正确:

-(instancetype)init{

self =[super init]

if(self)

{

}

return self;

}


2.Instance variable used while 'self' is not set to the result of '[(super or self) init...]'

self 未被赋值就使用了实例变量

错误:-(id)initWithIdentifier:(NSString*)identifier

{

if(self== [superinit]) {

_cellIdentifier= identifier;

}

returnself;

}

正确:-(id)initWithIdentifier:(NSString*)identifier

{

self= [super init];

if(self) {

_cellIdentifier= identifier;

}

return self;

}


3.The ‘viewWillAppear:’ instance method in UIViewController subclass ‘YourViewController’ is missing a [super viewWillAppear:] call

实例化子类YourViewController 未调用[super viewWillAppear:]

错误:-(void)viewWillAppear:(BOOL)animated

{


}

正确:

-(void)viewWillAppear:(BOOL)animated

{

[super viewWillAppear:animated];


}


4.Undefined or garbage value returned to caller 返回了垃圾值或未被定义的值。

//分区头高度

错误:-(CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section

{

CGFloat headerHeight

return headerHeight;

}


正确:-(CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section

{

CGFloat headerHeight = 0.000001;

return headerHeight;

}


5.The left expression of the compound assignment is an uninitialized value. The computed value will also be garbage 复合表达式 左边的值未被初始化,计算了也是垃圾值

错误:+(float)folderSizeAtPath{

float folderSize

folderSize+=4;

return folderSize;

}

正确:+(float)folderSizeAtPath{

float folderSize = 0;

folderSize+=4;

return folderSize;

}

6.Passed-by-value struct argument contains uninitialized data 参数中包含了未被初始化的结构体

错误:- (NSRange *)firstParagraph

{

NSRange range ;

return [self range:range];

}

正确:- (NSRange *)firstParagraph

{

NSRange range =NSMakeRange(0,0);


return [self range:range];

}

你可能感兴趣的:(IOS之XCode静态分析)