• iOS性能优化笔记


    这边博客主要是学习 博主全栈工程狮 的《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文档具体上一句的实现机制。
    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;
    }
     
    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 
  • 相关阅读:
    flutter开发No toolchains found in the NDK toolchains folder for ABI with prefix: armlinuxandroideabi错误的解决方法
    android开发 [CXX1104] NDK from ndk.dir at androidSdk\ndk\21.1.6352462 had version [21.1.6352462] which disagrees with android.ndkVersion [21.0.6113669]错误的解决方法
    android开发如何让Parcelable的使用和Serializable一样简单的解决方法
    flutter开发Build failed due to use of deprecated Android v1 embedding错误的解决方法
    android开发kotlin编译出错Module was compiled with an incompatible version of Kotlin
    android开发让App永不闪退,永不crash的解决方法
    android开发突破Android P及以上反射调用隐藏API限制的解决方法
    flutter开发The ADB at "androidSdk\platformtools\adb.exe" is too old; please install version 1.0.39 or later.错误的解决方法
    Android开发Android Gradle plugin requires Java 11 to run. You are currently using Java 1.8.错误的解决方法
    android开发以调试模式启动app方便调试启动时的代码
  • 原文地址:https://www.cnblogs.com/dlx-Blog/p/4337580.html
Copyright © 2020-2023  润新知