当前位置: 移动技术网 > 移动技术>移动开发>IOS > iOS 警告消除(记录贴)

iOS 警告消除(记录贴)

2020年08月14日  | 移动技术网移动技术  | 我要评论

记录接手的项目优化遇到的警告信息和处理方式

1、清除一些需要忽略的警告

Targets -> Build Settings (选择All,搜索custom)-> Apple Clang - Custom Compiler Flags -> Other Warning Flags.

填入要忽略的警告类型
右键警告点击 Reveal in Log 查看报警类型
例如: -Wdocumentation,填入内容为 -Wno-documentation,在W后面添加"no-"。

参考:忽略xcode的警告方法

2、Pod中的第三方库指定编译目标不在Xcode范围内

The iOS deployment target ‘IPHONEOS_DEPLOYMENT_TARGET’ is set to 6.0, but the range of supported deployment target versions is 8.0 to 13.6.99.

将下述内容添加在PodFile的最下方,重新执行Pod install。

post_install do |installer|
    installer.pods_project.targets.each do |target|
        target.build_configurations.each do |config|
            config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '8.0'
        end
    end
end

参考:iOS小记–warning: The iOS deployment target is set to 6.0, but the range XXX

3、Pod第三方库的警告

在Podfile中加入 “inhibit_all_warnings!”

source 'https://github.com/CocoaPods/Specs.git'

platform :ios, '9.0'
inhibit_all_warnings!

target "xxxx" do
4、支付宝SDK的警告

(arm64) /Users/intfre/workspace/other/ios-msdk-git/AlipaySDK4Standard/AlipaySDK/Library/UTDID.framework/UTDID(AidManager.o) unable to open object file: No such file or directory

  1. Go to Build Settings -> Build Options -> Debug Information Format

  2. Change the Debug setting from “DWARF with dSYM File” to “DWARF”

  3. Leave the Release setting at “DWARF with dSYM File”

参考:集成支付宝报警告

5、Pod中AFNetworking警告

MobileCoreServices has been renamed. Use CoreServices instead.

提示已经很明确了,按提示操作。

6、Launch images警告

Launch images are deprecated in iOS 13.0. Use a launch storyboard or XIB instead.

Assets里面移除Launch images,使用LaunchScreen;
Build Setting里搜索LaunchImages -> Asset Catalog Launch Image Set Name -> 移除LaunchImages

7、block的参数没有设置也没有写void报警告

This block declaration is not a prototype

Build Setting -> 搜索 Strict Prototypes 设置成NO。
block不设置参数的话,可以传任意个任意类型参数,设置成void就真的不能传参数了。有时候确实需要设计成传任意参数的,消除警告目前没有发现有什么不妥的地方,旧项目999+的错误,直接不提示好了。

8、block里面调用参数没有用self指针报警告

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

Build Setting -> 搜索 Implicit retain of 'self' within blocks 设置成NO。
Block里面避免循环引用,最好用weakself调用参数,写代码时养成习惯,顺手写上肯定不会出错。旧项目警告太多,运行稳定,就直接消除警告吧。

参考:xcode编译后警告的解决方法

9、误用逗号标点符号

possible misuse of comma operator here [-Wcomma]
free(coordinates), coordinates = NULL;

改掉。

10、 传参类型问题

Incompatible pointer types sending ‘NSArray *’ to parameter of type ‘NSMutableArray * _Nonnull’

改掉,方法里传NSArray就好了,因为NSMutableArray继承自NSArray,方法里面需要用可变对象的话,mutableCopy下。

11、使用YYCache缓存对象,实现了NSCoding的协议方法,但是没有在@interface 后面写报警告

Sending ‘xxx *’ to parameter of incompatible type ‘id _Nullable’

加上。

12、没有实现delegate里的方法

Class ‘xxxViewController’ does not conform to protocol ‘xxxDelegate’

实现对应方法,或者不需要实现的话在delegate非必须实现的方法上面添加@optional。

13、没有在interface后面声明UIGestureRecognizerDelegate协议

Assigning to ‘id _Nullable’ from incompatible type ‘xxxViewController *const __strong’

加上。

14、实现弃用的方法

Implementing deprecated method

看方法的适用范围,和自己项目支持的最低版本,决定删除还是区分版本处理。

15、performSelector调用警告

performSelector may cause a leak because its selector is unknown

看代码,Selector是个参数,编译器无法确定这个方法存在,所以报警告

[_delegate performSelector:_shareClick withObject:[NSNumber numberWithInteger:button.tag - 70]];

暂时消除警告,当然要自己确定这个方法存在,否则,自己试一下吧。

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
[_delegate performSelector:_shareClick withObject:[NSNumber numberWithInteger:button.tag - 70]];
#pragma clang diagnostic pop

参考:爆栈热门 iOS 问题

16、#import结尾加了分号

Extra tokens at end of #import directive

这个。。。删了分号,不知道怎么会加分号,还不止一个类,这是某位仁兄要留下自己的标记吗。还是我无知,有什么不知道的高级含义。

17、注释里的警告

Documentation Issue Group

注释里有个<a> 报警告
HTML tag ‘a’ requires an end tag

使用@param options说明方法里的参数但是方法里没有这个参数报警告
Parameter ‘options’ not found in the function declaration
这个字段后面没有说明内容报警告
Empty paragraph passed to ‘@param’ command

在 Build Settings 搜索 Other Warning Flags 填入 -Wno-documentation。
警告类型为-Wdocumentation,W后面加上no-,表示不显示这种类型警告。
我们自己在写代码写注释时最好写清楚各个字段含义,修改时注释内容一并改掉

18、字符串类型转换警告

Format String Issue Group

代码中int型的值转字符串使用了@"%zd", i,zd我也是第一次见到,参考这篇文章吧
printf中的format格式 %zd

int型直接用%d就好了。

给一个参数传了空值报警告
Null passed to a callee that requires a non-null argument

@property (nonatomic, strong) NSDictionary * dicData;
self.dicData = nil;

加个可为空的标识可消除警告。

@property (nonatomic, strong) NSDictionary *_Nullable dicData;

本文地址:https://blog.csdn.net/u011248221/article/details/107937408

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网