iOS 开发每天分享优质文章人猿星球iOS锦囊

iOS 开发项目警告汇总-长期更新

2019-06-28  本文已影响61人  lilei5

在我们进行iOS的开发中,项目警告由于不影响程序的编译和运行,所以常常被我们忽略。但是有可能这个警告在以后会造成意想不到的坑,所以在日常开发中,我们要将警告当做错误处理。尽量达到0警告的项目。

一、忽略警告

在开发中,不是所有的警告都是对我们有意义的,我们需要忽略这部分无意义的警告,把精力放在那些我们需要关注的地方。

1.忽略cocoapod项目中的警告

可以在Podfile文件里加入下面这段命令

inhibit_all_warnings!

注意:如果工程中有用到ReactiveCocoa这个库的话,忽略警告会产生错误,我们可以通过指定ReactiveCocoa不忽略警告解决。

pod 'ReactiveCocoa', ' 2.5', :inhibit_warnings => true

2、xcode8之后会,文本注释有时会产生警告,我们可以通过在 Build Settings里搜索 Documentation Comments
文本注释去除警告
3、通过宏忽略指定类型警告
//常见警告名称
//1, 声明变量未使用      "-Wunused-variable"
//2, 方法定义未实现      "-Wincomplete-implementation"
//3, 未声明的选择器      "-Wundeclared-selector"
//4, 参数格式不匹配      "-Wformat"
//5, 废弃掉的方法        "-Wdeprecated-declarations"
//6, 不会执行的代码      "-Wunreachable-code"
//7, 非严格的类型声明    "-Wstrict-prototypes"
#pragma clang diagnostic push
#pragma clang diagnostic ignored "警告名称"
 
// 被夹在这中间的代码针对于此警告都会忽视不会显示出来
 
#pragma clang diagnostic pop

例如当我们通过performSelector产生的leaks警告:

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
           [target performSelector:action withObject:params];
#pragma clang diagnostic pop
4、Xcode9之后,如果定义类似这样的block如typedef void (^myBlock)();,会出现如下警告

This block declaration is not a prototype

可以修改为typedef void (^myBlock)(void);或者使用上面的宏"-Wstrict-prototypes"来忽略警告,如果项目中这种类型很多,且第三方库里也有类似警告,可以在targets --> build settings -->strict Prototypes 修改为NO

image.png

二、手动处理各种警告

Unused variable 'abc'

'H' macro redefined

Implicit conversion loses integer precision: 'NSInteger' (aka 'long') to 'int'

Capturing 'self' strongly in this block is likely to lead to a retain cycle

Method possibly missing a [super awakeFromNib] call

'xxxxxx' is deprecated: first deprecated in iOS XX - Use -xxxxxx

"directory not found for option '-L/...'"
解决方法: Project -> targets -> Build Setting -> Library Search Paths 删除报警告的路径

"directory not found for option '-F/..."
解决方法:Project -> targets -> Build Setting -> Framework Search Paths删除报警告的路径

Auto property synthesis will not synthesize property 'name'; it will be implemented by its superclass, use @dynamic to acknowledge intention

Block implicitly retains 'self'; explicitly mention 'self' to indicate this is intended behavior

The image set name "" is used by multiple image sets.

上一篇下一篇

猜你喜欢

热点阅读