这边博客主要是学习 博主全栈工程狮 的《IOS性能调优系列:Analyze静态分析》 后的实践,最近公司的项目上架并没有做性能的调优,故此在此记录,以便以后使用,在此感谢博主 全栈工程狮 写的精彩博文
(引用)Analyze主要分析以下四种问题:
1、逻辑错误:访问空指针或未初始化的变量等;
2、内存管理错误:如内存泄漏等;
3、声明错误:从未使用过的变量;
4、Api调用错误:未包含使用的库和框架。
进过测试存在的问题:
1. 未用过的变量
2. 在一个类的实例方法中,没有对类进行init就进行访问他的成员变量 出现
instance variable used while "self" is not set to the result of [(super or self) init
代码如下
- (id)initWithStyle:(HZAreaPickerStyle)pickerStyle withDelegate:(id <HZAreaPickerDelegate>)delegate
{
self = [[[NSBundle mainBundle] loadNibNamed:@"HZAreaPickerView" owner:self options:nil] objectAtIndex:0] ;
//self = [super init]; 添加这一句后就OK了,而且上面这一句不能跟下面这一句调换,调换就会出现一样的提示,如果没有上面这一句,直接写这一句就不会给错这样的问题,但是视图是没有载入,还有写在if的里面也是一样的报这样的问题。按照分析这里错误是属于第一类,也就是说类不进行init肯定是没法使用他的成员变量的,而且上面这一句方法是没有调用类的init方法的 只是简单的给一属性值,这里可以查看API文档具体上一句的实现机制。
{
self = [[[NSBundle mainBundle] loadNibNamed:@"HZAreaPickerView" owner:self options:nil] objectAtIndex:0] ;
//self = [super init]; 添加这一句后就OK了,而且上面这一句不能跟下面这一句调换,调换就会出现一样的提示,如果没有上面这一句,直接写这一句就不会给错这样的问题,但是视图是没有载入,还有写在if的里面也是一样的报这样的问题。按照分析这里错误是属于第一类,也就是说类不进行init肯定是没法使用他的成员变量的,而且上面这一句方法是没有调用类的init方法的 只是简单的给一属性值,这里可以查看API文档具体上一句的实现机制。
1. 载入的nib文件其实都是xml 文件,我们设计好之后被归档,然后调用上面的方法会进行解归档(在程序运行时期)
if (self) {
self.delegate = delegate;
self.pickerStyle = pickerStyle;
self.locatePicker.dataSource = self;
self.locatePicker.delegate = self;
//加载数据
if (self.pickerStyle == HZAreaPickerWithStateAndCityAndDistrict) {
provinces = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"ZH_area.plist" ofType:nil]];//province,city 都是声明的成员变量 ,并设置set,给方法
cities = [[provinces objectAtIndex:0] objectForKey:@"cities"];
self.locate.state = [[provinces objectAtIndex:0] objectForKey:@"provinceName"];
self.locate.stateID = [[provinces objectAtIndex:0] objectForKey:@"provinceId"];
self.locate.city = [[cities objectAtIndex:0] objectForKey:@"cityName"];
self.locate.cityID = [[cities objectAtIndex:0] objectForKey:@"cityId"];
areas = [[cities objectAtIndex:0] objectForKey:@"counties"];
if (areas.count > 0) {
self.locate.district = [[areas objectAtIndex:0] objectForKey:@"countyName"];
self.locate.districtID = [[areas objectAtIndex:0] objectForKey:@"countyId"];
} else{
self.locate.district = @"";
self.locate.districtID = @"";
}
} else{
provinces = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"ZH_area.plist" ofType:nil]];
cities = [[provinces objectAtIndex:0] objectForKey:@"cities"];
self.locate.state = [[provinces objectAtIndex:0] objectForKey:@"provinceName"];
self.locate.city = [[cities objectAtIndex:0] objectForKey:@"cityName"];
}
}
return self;
}
self.delegate = delegate;
self.pickerStyle = pickerStyle;
self.locatePicker.dataSource = self;
self.locatePicker.delegate = self;
//加载数据
if (self.pickerStyle == HZAreaPickerWithStateAndCityAndDistrict) {
provinces = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"ZH_area.plist" ofType:nil]];//province,city 都是声明的成员变量 ,并设置set,给方法
cities = [[provinces objectAtIndex:0] objectForKey:@"cities"];
self.locate.state = [[provinces objectAtIndex:0] objectForKey:@"provinceName"];
self.locate.stateID = [[provinces objectAtIndex:0] objectForKey:@"provinceId"];
self.locate.city = [[cities objectAtIndex:0] objectForKey:@"cityName"];
self.locate.cityID = [[cities objectAtIndex:0] objectForKey:@"cityId"];
areas = [[cities objectAtIndex:0] objectForKey:@"counties"];
if (areas.count > 0) {
self.locate.district = [[areas objectAtIndex:0] objectForKey:@"countyName"];
self.locate.districtID = [[areas objectAtIndex:0] objectForKey:@"countyId"];
} else{
self.locate.district = @"";
self.locate.districtID = @"";
}
} else{
provinces = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"ZH_area.plist" ofType:nil]];
cities = [[provinces objectAtIndex:0] objectForKey:@"cities"];
self.locate.state = [[provinces objectAtIndex:0] objectForKey:@"provinceName"];
self.locate.city = [[cities objectAtIndex:0] objectForKey:@"cityName"];
}
}
return self;
}
3. 没有正确使用CGImageRef,CGImageSourceRef 内存泄露 即使在ARC环境下也是如此 (注 : ARC只对NSObject有用)
CGImageRef 使用CGImageRelease 释放 ;CGImageSourceRef 使用 CFRelease 释放,但是要记得进行是否为NULL的判断
4. 在调用一些系统方法的时候忘记调用 [super ....]
5. 在block里面声明了NSString对象,会出现这个变量在实例化没有被引用的情况,block里面声明的都是弱的,弱视图调用就报这样的,解决办法,声明一个成员变量来存储block里面的内容
错误提示 : (代码被改了,不报错了,凭记忆写)variable storyed in "变量名"during its initation is never read