標籤:ios clang warning xcode
原創Blog,轉載請註明出處
blog.csdn.net/hello_hwc
歡迎關注我的iOS SDK詳解專欄,這裡有很多基礎的文章。
http://blog.csdn.net/column/details/huangwenchen-ios-sdk.html
前言:warnings是編碼中很重要的一個環節,編譯器給出合理的warning能協助開發人員找到自己代碼的問題,防止很多bug產生。
預設用XCode建立一個工程,會自動開啟一些重要的warnings,但是更多的時候,我們需要編譯器更完整的提醒。
iOS開發採用Clang編譯器。
預設的Warning可以在Build Settings裡找到
在search裡搜尋Warnings,就可以看到,這是為所有語言開啟的warnings
當然,也可以為不同語言開啟warning,也在Build Settings裡
但是,這樣一個個的開啟關閉定製化很好,有時候我們只需要開啟全部或者開啟全部重要的warnings即可
這時候,進入
可以添加一些build flag來啟用警告,主要的就是三個
1.-Wall Clang認為自己能夠準確報出的警告
2. -Wextra額外的苛刻的警告,這些警告不一定會造成錯誤。例如如果使用這個flag,把singned 賦值給unsigned就會觸發警告,而大多數時候這樣賦值是沒問題的。
3.-Weverything 所有警告
一般的項目都是開啟-Wall和-Wextra兩個警告來保證沒有嚴重錯誤,當然,如果有些明顯的不會出錯,可以用關閉某個或者某些警告。
-Wall -Wno-unused-variable //啟用Wall但是剔除unused-variable
用語句強制開啟或者關閉某個警告
強制開啟一個警告
#warning "This method can not be used"
強制開啟一個錯誤
#error "You must add this key,or you will fail"
強制關閉一個警告
例如
這裡會出現警告test這個selector沒有實現
[self performSelector:@selector(test) withObject:nil];
強制關閉這個警告
#pragma clang diagnostic push#pragma clang diagnostic ignored "-Wundeclared-selector" [self performSelector:@selector(test) withObject:nil];#pragma clang diagnostic pop
常用的CLang Warning
- Wall
- Wbad-function-cast
- Wcast-align
- Wconversion
- Wdeclaration-after-statement
- Wdeprecated-implementations
- Wextra
- Wfloat-equal
- Wformat=2
- Wformat-nonliteral
- Wfour-char-constants
- Wimplicit-atomic-properties
- Wmissing-braces
- Wmissing-declarations
- Wmissing-field-initializers
- Wmissing-format-attribute
- Wmissing-noreturn
- Wmissing-prototypes
- Wnested-externs
- Wnewline-eof
- Wold-style-definition
- Woverlength-strings
- Wparentheses
- Wpointer-arith
- Wredundant-decls
- Wreturn-type
- Wsequence-point
- Wshadow
- Wshorten-64-to-32
- Wsign-compare
- Wsign-conversion
- Wstrict-prototypes
- Wstrict-selector-match
- Wswitch
- Wswitch-default
- Wswitch-enum
- Wundeclared-selector
- Wuninitialized
- Wunknown-pragmas
- Wunreachable-code
- Wunused-function
- Wunused-label
- Wunused-parameter
- Wunused-value
- Wunused-variable
- Wwrite-strings
困難模式
所謂的困難模式就是開啟所有警告,並且把警告當作error。這意味著,只要還有一個警告存在,那麼程式將不能編譯運行。
參考連結
http://onevcat.com/2013/05/talk-about-warning/
http://programmers.stackexchange.com/questions/122608/clang-warning-flags-for-objective-c-development
http://amattn.com/p/better_apps_clang_weverything_or_wall_is_a_lie.html
http://nshipster.cn/clang-diagnostics/
iOS XCode啟用/關閉Clang Warnings