去掉xcode中警告的一些经验
1、編譯時(shí),編譯警告忽略掉某些文件
? ? ? 只需在在文件的Compiler Flags 中加入 -w 參數(shù),例如:
2、編譯時(shí),編譯警告忽略掉某段代碼
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wmultichar"
? ?char b = 'df'; // no warning.
#pragma clang diagnostic pop
參考網(wǎng)址:http://stackoverflow.com/questions/7897429/ignore-all-warnings-in-a-specific-file-using-llvm-clang/8087544#8087544
3、編譯時(shí),analyse警告忽略掉某些文件
? ?只需在文件的Compiler Flags 中加入?-Xanalyzer -analyzer-disable-checker?參數(shù),例如:
參考網(wǎng)址:http://stackoverflow.com/questions/7897429/ignore-all-warnings-in-a-specific-file-using-llvm-clang
4、編譯時(shí),analyse警告忽略掉某段代碼
#ifndef?__clang_analyzer__
? ? ? // Code not to be analyzed
#endif
? 參考網(wǎng)址:http://stackoverflow.com/questions/5806101/is-it-possible-to-suppress-xcode-4-static-analyzer-warnings
5、項(xiàng)目使用arc以后,調(diào)用[someTarget performSelector:someAction]會(huì)報(bào)警告,有如下三種解決方法:
? ? ?a、當(dāng)ARC檢查警告時(shí),忽略掉該段代碼? ? ? ? ? ??
????#pragma clang diagnostic push????
? ?#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
? ?????[object performSelector:action];????
? ?#pragma clang diagnostic pop
? ?
? ? 對(duì)于多處出現(xiàn)該警告時(shí),可以定義一個(gè)宏來(lái)替換,比如
#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函數(shù)進(jìn)行替換
? ? ? ? ??#import <objc/message.h>
? ? ? ? ??objc_msgSend(object, action);
? ? ? c、在該代碼文件的的Compiler Flags 中加入-Wno-arc-performSelector-leaks?參數(shù)
? 參考網(wǎng)址:http://stackoverflow.com/questions/7017281/performselector-may-cause-a-leak-because-its-selector-is-unknown/7073761#7073761
6、對(duì)于category覆蓋類里面方法導(dǎo)致的警告,可能就要修改源代碼了。因?yàn)樘O果是不建議在category中覆蓋類方法的,以為這種行為會(huì)產(chǎn)生未知的結(jié)果。
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.
參考網(wǎng)址: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、對(duì)于某個(gè)類中存在方法名和系統(tǒng)某個(gè)類的方法名相同的情形,如果你在此類的靜態(tài)方法中使用self來(lái)調(diào)用該方法,可能引發(fā)警告,所以盡量避免此種情況。比如
我自定義一個(gè)類 RequestTask 繼承自NSObject,里面有個(gè)靜態(tài)方法:
+ (id)taskWithRequest:(BaseRequest *)request delegate:(id)delegate
{
? ? return [[self alloc] initWithRequest:request delegate:delegate];
}
而在我的RequestTask確實(shí)有一個(gè)方法的定義為:
- (id)initWithRequest:(BaseRequest *)req delegate:(id)delegate;
理論上講這個(gè)是沒(méi)有任何問(wèn)題的,但是編譯器編譯的時(shí)候卻有一個(gè)警告,因?yàn)镹SURLConnection有一個(gè)相同的方法,編譯器認(rèn)為我調(diào)用的是NSURLConnection類的該方法,參數(shù)類型不對(duì)報(bào)錯(cuò)。
所以此種情況,我們應(yīng)該避免直接在靜態(tài)方法中使用self調(diào)用類的實(shí)例方法。
8、當(dāng)使用兩個(gè)不匹配的enum類型或者enum類型默認(rèn)也是會(huì)報(bào)警告的,此種情況可以通過(guò)直接強(qiáng)制類型轉(zhuǎn)換解決,也可以在編譯器中規(guī)避掉此種警告。例如:
9、當(dāng)Enum類型和Enum類型中未定義的整形范圍進(jìn)行比較時(shí),編譯器也會(huì)給警告。此種解決方法目前查到的就是強(qiáng)制類型轉(zhuǎn)化(如果有其他方式,請(qǐng)看到的ggjj告訴我一下,再此謝過(guò)了)
轉(zhuǎn)載于:https://www.cnblogs.com/zsw-1993/p/4879405.html
總結(jié)
以上是生活随笔為你收集整理的去掉xcode中警告的一些经验的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: ACM 会场安排问题
- 下一篇: js/css 检测移动设备方向的变化 判