如何保障iOS代码#高质量#之警告设置

1,856 阅读4分钟
  • 首先看一个常见的警告 -Wunguarded-availability-new

-Wunguarded-availability-new.png

  • 在Build Setting 里的OTHER_CFLAGS添加-Werror=unguarded-availability-new
  • 注意是以 -Werror=开头
  • 再编译看是否能变Error

aaa.png

  • 如愿以偿

参考链接

常见的警告

(1)-Wunguarded-availability(在工程中设置了忽略)

警告提示:warning: 'setResumingHandler:' is only available on iOS 9.0 or newer [-Wunguarded-availability]

警告原因:使用了支持的所有iOS版本不兼容的api时会发出警告

(2)-Wunknown-warning-option

警告提示:warning: unknown warning group '-Wreceiver-is-weak', ignored [-Wunknown-warning-option]

警告原因:使用了-Wreceiver-is-weak警告选项

(3)-Wstrict-prototypes(在工程中设置了忽略)

警告提示:warning: this block declaration is not a prototype [-Wstrict-prototypes] 或者 warning: this function declaration is not a prototype [-Wstrict-prototypes]

警告原因:在block或者函数定义,参数为空没写void时会发出警告;这个设置忽略的原因是因为,block的参数为空写为()时,可以接受数量不定的参数,有些第三库是故意这样写的。 

(4)-Wsometimes-uninitialized

警告提示:warning: variable 'overshootPoint' is used uninitialized whenever 'if' condition is false [-Wsometimes-uninitialized]

警告原因:变量在某些情况下可能未被初始化时发出警告 

(5)-Wobjc-protocol-method-implementation

警告提示:warning: category is implementing a method which will also be implemented by its primary class [-Wobjc-protocol-method-implementation]

警告原因:在类别中重写原类方法时发出警告

(6)-Wobjc-literal-conversion

警告提示:warning: object of type 'NSNumber *' is not compatible with dictionary value type 'NSString *' [-Wobjc-literal-conversion]

警告原因:赋值类型不匹配时发出警告

(7)-Wmacro-redefined

警告提示:warning: 'MARGIN' macro redefined [-Wmacro-redefined]

警告原因:宏定义重复定义时发出警告 

(8)-Wobjc-designated-initializers

警告提示:warning: method override for the designated initializer of the superclass '-initWithFrame:collectionViewLayout:' not found [-Wobjc-designated-initializers]

警告原因:在初始化方法的定义后面添加了NS_DESIGNATED_INITIALIZER宏,没有满足指定构造器的条件时会发出警告 

(9)-Wnonnull

警告提示:warning: null passed to a callee that requires a non-null argument [-Wnonnull]

警告原因:null传递给非空参数时发出警告

(10)-Wunused-getter-return-value

警告提示:warning: property access result unused - getters should not be used for side effects [-Wunused-getter-return-value]

(11)-Wincomplete-implementation

警告提示:warning: method definition for 'modifyAddress:' not found [-Wincomplete-implementation]

警告原因:在.h定义了,没有在.m实现时发出警告

(12)-Wunused-variable

警告提示:warning: unused variable 'lineSpacing' [-Wunused-variable]

警告原因:变量没有使用时,发出警告 

(13)-Warc-performSelector-leaks (在工程中设置了忽略)

警告提示:warning: performSelector may cause a leak because its selector is unknown [-Warc-performSelector-leaks]

警告原因:使用- (id)performSelector:(SEL)aSelector;方法,当aSelector未知时会发出警告

(14)-Undeclared-Selector (在工程中设置了忽略)

警告提示:warning: undeclared selector 'rewardRuleTouched:' [-Wundeclared-selector]

警告原因:使用@selector(rewardRuleTouched:)时,如果rewardRuleTouched:方法在当前类没有声明则发出警告

(15)-Wobjc-literal-conversion

警告提示:warning: implicit boolean conversion of Objective-C object literal always evaluates to true [-Wobjc-literal-conversion]

警告原因:把oc对象字面量赋值给bool类型时总是为true,工程中有这种情况时发出警告 

(16)-Wnon-literal-null-conversion

警告提示:warning: expression which evaluates to zero treated as a null pointer constant of type 'NSString * _Nullable' [-Wnon-literal-null-conversion]

(17)-Wproperty-attribute-mismatch

警告提示:warning: 'copy' attribute on property 'tailCode' does not match the property inherited from 'RouteDetailProtocol' [-Wproperty-attribute-mismatch]

警告原因:当代理方实现的@property属性和委托方定义的描述不一致时,发出警告

(18)-Wincompatible-pointer-types

警告提示:warning: incompatible pointer types assigning to 'TestDetailDateChooseCell *' from 'UITableViewCell *' [-Wincompatible-pointer-types]

警告原因:赋值时,等号两边指针类型不一致时发出警告 

(19)-Wnullability-completeness

警告提示:warning: pointer is missing a nullability type specifier (_Nonnull, _Nullable, or _Null_unspecified) [-Wnullability-completeness]

警告原因:@propery描述的属性缺少nullability类型的修饰时发出警告 
 

(20)-Wprotocol

警告提示:warning: class 'TestDetailViewController' does not conform to protocol 'TestDetailPresenterProtocol' [-Wprotocol]



警告原因:没有实现尖括号中协议的方法时发出警告

(21)-Wobjc-missing-super-calls

警告提示:warning: method possibly missing a [super prepareForReuse] call [-Wobjc-missing-super-calls]

警告原因:缺少父类实现时发出警告

(22)-Wdocumentation

警告提示:warning: empty paragraph passed to '@param' command [-Wdocumentation]

警告原因:警告相关的警告  

(23)-Wformat

警告提示:warning: format specifies type 'long' but the argument has type 'int'

(24)-Wshorten-64-to-32

警告提示:warning: implicit conversion loses integer precision: 'long long' to 'NSInteger' (aka 'int') [-Wshorten-64-to-32]

(25)-Wdeprecated-declarations

警告提示:warning: 'SSDKSetupSinaWeiboShareParamsByText:title:image:url:latitude:longitude:objectID:type:' is deprecated: discard form v4.0.4

(26)-Wint-conversion

警告提示:warning: incompatible pointer to integer conversion initializing 'BOOL' (aka 'signed char') with an expression of type 'NSString *'