近期整理了一些Xcode的常见编译警告,文末还有如何屏蔽消除编译警告的办法。
整理的编译警告所属分类有:Warning、Format String Issue、Unused Entity Issue、Semantic Issue、Value Conversion Issue、Nullability Issue、Lexical or Preprocessor Issue、Target Integrity、Validate Project Settings、Localizability Issue (Apple) 、Dead store、Logic error、Memory error、Core Foundation/Objective-C、API Misuse (Apple)
1、The iOS Simulator deployment target ‘IPHONEOS_DEPLOYMENT_TARGET’ is set to 8.0, but the range of supported deployment target versions is 9.0 to 14.0.99.
在podfile 最下方加上以下代码
post_install do |installer|
installer.pods_project.targets.each do |target|
target.build_configurations.each do |config|
if config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'].to_f < 8.0
config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '8.0'
end
end
end
end
然后pod install
2、MobileCoreServices has been renamed. Use CoreServices instead.
把Pods中的第三方框架所依赖的 MobileCoreService 换成 CoreService 就可以。或者在Validate Workspace – Ignored Frameworks中增加MobileCoreService。
更详细的请参考:https://www.jianshu.com/p/020e39fae7b4
3、AssetsLibrary is deprecated. Consider migrating to Photos instead.
AssetsLibrary已弃用,使用Photos代替。
4、Method definition for ‘setModel:’ not found
头文件中声明了setModel:方法,但未实现该方法。增加实现或删除声明。
5、Leading or Trailing horizontal alignment before iOS 11.0 [5]
使用标准的left / rigth 对齐方式的功能只支持ios11以上版本,对于低版本是不支持的,只能使用 leading / trailing 左右边距对齐的方式。所以要把left改成leading。
6、“Master“ is unreachable because it has no entry points, and no identifier for runtime access via -[UIStoryboard instantiateViewControllerWithIdentifier:]. [9]
Main.storyboard中存在controller未设置为entry points,也没有设置identifier,Storyboard ID。即是一个没使用的VC。设置identifier或者删除掉。
7、Possible misuse of comma operator here
这里可能误用了逗号运算符,请检查代码
8、Class ‘XXXViewController’ does not conform to protocol ‘XXXViewDelegate’
未实现XXXViewDelegate方法,实现相关代理方法
9、Incompatible pointer types assigning to ‘NSMutableArray *’ from ‘NSArray *’
将一个NSArray对象赋值给NSMutableArray对象。
10、Incompatible pointer types assigning to ‘NSString * _Nonnull’ from ‘NSDecimalNumber *’
将一个NSDecimalNumber对象复制给NSString对象。
11、Block implicitly retains ‘self’; explicitly mention ‘self’ to indicate this is intended behavior
Block implicitly retains ‘self’; explicitly mention ‘self’ to indicate this is intended behavior,在block内使用_方式访问属性会出现这个提示,改成self.的形式即可。
12、 Assigning to ‘id<UINavigationControllerDelegate,UIImagePickerControllerDelegate> _Nullable’ from incompatible type ‘XXXViewController *const __strong’
UIImagePickerController要遵循UINavigationControllerDelegate和UIImagePickerControllerDelegate,增加即可。
13、Assigning to ‘id<MMCropDelegate>’ from incompatible type ‘XXXXController *const __strong’
.h中增加MMCropDelegate即可
14、Code will never be executed
该部分代码不会被执行
15、Unused variable ‘weakself’
声明的变量未使用,删除无用变量或检查代码是否漏掉了。
16、Unused variable ‘errorMessage’
声明的变量未使用,删除无用变量或检查代码是否漏掉了。
17、Pointer is missing a nullability type specifier (_Nonnull, _Nullable, or _Null_unspecified)
属性声明未增加_Nonnull、_Nullable、_Null_unspecified
18、Block pointer is missing a nullability type specifier (_Nonnull, _Nullable, or _Null_unspecified)
block属性未增加_Nonnull、_Nullable、_Null_unspecified
19、Format specifies type ‘int’ but the argument has type ‘unsigned long’
[NSString stringWithFormat:@”%d”, [ticketFee intValue]/passengerInfoArray.count]; 格式化错误
20、Values of type ‘NSUInteger’ should not be used as format arguments; add an explicit cast to ‘unsigned long’ instead
NSString格式化错误
21、Values of type ‘NSInteger’ should not be used as format arguments; add an explicit cast to ‘long’ instead
NSString格式化错误
22、Missing terminating ‘”‘ character
产生代码:#import “XXXView.h””,末尾多了一个引号,删除多余的引号
23、Extra tokens at end of #import directive
产生代码:#import “XXXView.h””,末尾多了一个引号,删除多余的引号
24、Localizability Issue (Apple) :User-facing text should use localized string macro
字符串没有使用本地化的字符串宏,可修改Target-Build Settings,Missing Localizability为NO
25、Value stored to ‘height’ is never read
height从未使用,请检查代码
26、Value stored to ‘xxString’ during its initialization is never read
类似:NSArray *array = [NSArray new];array = @[@“awnlab.com”]; 是因为在一开始初始化的时候定义了一个内存空间,可是在下面的时候直接赋值了,并没有使用一开始使用的那个堆内存中的对象。可修改为NSArray *array =nil;
27、Division by zero
存在除0可能性,增加除数非0判断
28、 Undefined or garbage value returned to caller (within a call to ‘GetMatchValue’)
返回值是个未定义或垃圾值,请检查代码
29、Property of mutable type ‘NSMutableArray<ZLPhotoModel *>’ has ‘copy’ attribute; an immutable object will be stored instead
产生代码:@property (nonatomic, copy) NSMutableArray<ZLPhotoModel *> *arrSelectedModels; 可变数组NSMutableArray属性使用了copy,改为strong,或者检查代码是否可用NSArray替换
30、The left operand of ‘<<‘ is a garbage value
请检查代码
31、nil returned from a method that is expected to return a non-null value
方法返回参数不可为nil,但实际返回了nil。修改返回参数声明为_Nullable
32、nil passed to a callee that requires a non-null 3rd parameter
在方法声明时,第三个参数不可为空,但在调用时传了个可能为空的参数。入参增加非nil判断,或者修改方法参数声明增加_Nullable
33、nil passed to a callee that requires a non-null 1st parameter
在方法声明时,第一个参数不可为空,但在调用时传了个可能为空的参数。入参增加非nil判断,或者修改方法参数声明增加_Nullable
34、Instance variable used while ‘self’ is not set to the result of ‘[(super or self) init…]’
在init有关的初始化方法中,必须先使用 [super init]来获取self。
35、The ‘viewDidLoad’ instance method in UIViewController subclass ‘CropImageViewController’ is missing a [super viewDidLoad] call
重写了viewDidLoad方法,但viewDidLoad方法中没有调用[super viewDidLoad],增加[super viewDidLoad]调用。
36、Array element cannot be nil
数组Array元素不能为nil,增加非nil判断。
37、Dictionary value cannot be nil
Dictionary中的value不能为nil,增加非空判断。
38、Update to recommended settings
建议更新配置,点开Perform Changes即可。是因为开启了Suspicious Moves编译警告。可在Build Settings中搜索Suspicious Moves就可看到了。
39、Capturing ‘self’ strongly in this block is likely to lead to a retain cycle
block里引用了self,将会产生循环引用,将self改成weakself
40、IB Designables: Failed to render and update auto layout status for
41、’setOverrideUserInterfaceStyle:’ is only available on iOS 13.0 or newer
13.0以上系统可用,增加系统版本判断,if (@available(iOS 13.0, *))
42、Auto Layout Localization: Leading constraint is missing, which may cause overlapping with other views.
相关约束丢失,clear重试
43、propertyListFromData:mutabilityOption:format:errorDescription:’ is deprecated: first deprecated in iOS 8.0 – Use propertyListWithData:options:format:error: instead.
方法已废弃,请使用新方法代替。
44、Type specifier missing, defaults to ‘int’
通常出现在如下代码中:
typedef void(^DateTimeBolck)(NSString *timeString,userExpirationDate);
定义了一个block,第二个参数未指定类型,则会出现该提示,默认将会是int类型。
45、Variable ‘uuidArray’ was never mutated; consider changing to ‘let’ constant
Replace ‘var’ with ‘let’
如何屏蔽或消除警告呢?
既然有这么多的警告,有些警告无需解决,但又想屏蔽掉消除掉,那么你可以点击警告信息,选择Reveal In Log,就可以看到警告的详细信息,在警告后面中括号中的就是要使用的关键信息,例如:warning: unused variable ‘weakself’ [-Wunused-variable]
然后我们可以使用如下方法进行隐藏:
#pragma clang diagnostic push
#pragma clang diagnostic ignored “上面提到的中括号中的关键信息”
//your code
#pragma clang diagnostic pop
例如针对unused variable ‘weakself’ [-Wunused-variable],可采用:
#pragma clang diagnostic push
#pragma clang diagnostic ignored “-Wunused-variable”
//your code
#pragma clang diagnostic pop
如何批量处理呢?毕竟有时候相同警告有很多,一个一个进行修改也挺费劲。
按文件修改:可以在Compile Sources中设置某个文件的 Compiler Flags,将上面得到的关键信息进行修改,例如得到的 warning 信息是 -Wunused-variable 需要改成 -Wno-unused-variable
即所有的这类报错都是 -W错误信息 的格式,我们需要将 -W 替换成 -Wno- 即告诉编译器这个文件的这个错误不再提示警告,可以添加多个信息。
按工程设置:在Build Settings中搜索Other Warning Flags,可在Debug或Release中进行修改,样式类似:-Wno-unused-variable,同样可以添加多个。(不建议按工程进行配置,毕竟编译警告都是可能存在问题的地方)