1、編譯時,編譯警告忽略掉某些文件
? ? ? 只需在在文件的Compiler Flags 中加入 -w 參數,例如:
2、編譯時,編譯警告忽略掉某段代碼
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wmultichar"
? ?char b = 'df'; // no warning.
#pragma clang diagnostic pop
參考網址:http://stackoverflow.com/questions/7897429/ignore-all-warnings-in-a-specific-file-using-llvm-clang/8087544#8087544
3、編譯時,analyse警告忽略掉某些文件
? ?只需在文件的Compiler Flags 中加入?-Xanalyzer -analyzer-disable-checker?參數,例如:
參考網址:http://stackoverflow.com/questions/7897429/ignore-all-warnings-in-a-specific-file-using-llvm-clang
4、編譯時,analyse警告忽略掉某段代碼
#ifndef?__clang_analyzer__
? ? ? // Code not to be analyzed
#endif
? 參考網址:http://stackoverflow.com/questions/5806101/is-it-possible-to-suppress-xcode-4-static-analyzer-warnings
5、項目使用arc以后,調用[someTarget performSelector:someAction]會報警告,有如下三種解決方法:
? ? ?a、當ARC檢查警告時,忽略掉該段代碼? ? ? ? ? ??
????#pragma clang diagnostic push????
? ?#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
? ?????[object performSelector:action];????
? ?#pragma clang diagnostic pop
? ?
? ? 對于多處出現該警告時,可以定義一個宏來替換,比如
#define NoWarningPerformSelector(target, action, object) \
_Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Warc-performSelector-leaks\"") \
[target performSelector:action withObject:object]; \
_Pragma("clang diagnostic pop") \
? ? ? b、使用objc_msgSend函數進行替換
? ? ? ? ??#import <objc/message.h>
? ? ? ? ??objc_msgSend(object, action);
? ? ? c、在該代碼文件的的Compiler Flags 中加入-Wno-arc-performSelector-leaks?參數
? 參考網址:http://stackoverflow.com/questions/7017281/performselector-may-cause-a-leak-because-its-selector-is-unknown/7073761#7073761
6、對于category覆蓋類里面方法導致的警告,可能就要修改源代碼了。因為蘋果是不建議在category中覆蓋類方法的,以為這種行為會產生未知的結果。
If the name of a method declared in a category is the same as a method in the original class, or a method in another category on the same class (or even a superclass), the behavior is undefined as to which method implementation is used at runtime.
參考網址:http://stackoverflow.com/questions/13388440/xcode-4-5-warns-about-method-name-conflicts-between-categories-for-parent-child
https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/CustomizingExistingClasses/CustomizingExistingClasses.html ?(Avoid Category Method Name Clashes段落)
7、對于某個類中存在方法名和系統某個類的方法名相同的情形,如果你在此類的靜態方法中使用self來調用該方法,可能引發警告,所以盡量避免此種情況。比如
我自定義一個類 RequestTask 繼承自NSObject,里面有個靜態方法:
+ (id)taskWithRequest:(BaseRequest *)request delegate:(id)delegate
{
? ? return [[self alloc] initWithRequest:request delegate:delegate];
}
而在我的RequestTask確實有一個方法的定義為:
- (id)initWithRequest:(BaseRequest *)req delegate:(id)delegate;
理論上講這個是沒有任何問題的,但是編譯器編譯的時候卻有一個警告,因為NSURLConnection有一個相同的方法,編譯器認為我調用的是NSURLConnection類的該方法,參數類型不對報錯。
所以此種情況,我們應該避免直接在靜態方法中使用self調用類的實例方法。
8、當使用兩個不匹配的enum類型或者enum類型默認也是會報警告的,此種情況可以通過直接強制類型轉換解決,也可以在編譯器中規避掉此種警告。例如:
9、當Enum類型和Enum類型中未定義的整形范圍進行比較時,編譯器也會給警告。此種解決方法目前查到的就是強制類型轉化(如果有其他方式,請看到的ggjj告訴我一下,再此謝過了)