警告: Category is implementing a method which will also be implemented by its primary class
警告
Category is implementing a method which will also be implemented by its primary class
這個警告的意思是, 在category中重寫了原類的方法
分析
A category allows you to add new methods to an existing class. If you want to reimplement a method that already exists in the class, you typically create a subclass instead of a category.
蘋果官方文檔Customizing existing classes中如下描述:
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.
這個警告說明, 類目中添加的這個方法和原類的方法名一致, 運行的時候會執行這個方法, 而且也會執行原類中的方法. 蘋果官方文檔中又說明,如果在類別中聲明的方法的名稱與原始類中的方法相同,或者在同一類(或甚至超類)上的另一類中的方法相同,那么該行為對于使用哪種方法實現是未定義的運行。 如果您使用自己的類使用類別,那么這不太可能成為問題,但是在使用類別添加標準Cocoa或Cocoa Touch類的方法時可能會導致問題。
解決方案
1>代碼方式
2>Xcode設置方式:
在target的 build settings下 搜索other warning flags 然后給其添加 -Wno-objc-protocol-method-implementation
注: 這里的警告忽略的處理方法不能改變原警告造成的問題, 只能是屏蔽掉了警告, 所以當前方法和原類的方法都還是會執行的.
拓展
在忽略警告的處理上, 你可以在項目運行的時候, 右鍵警告選擇 reveal in log 就可以在警告詳情中發現 -Wobjc-protocol-method-implementation 這么一個格式的字段 在-W后添加一個no- ,然后添加到 other warning flags 中 就可以忽略你這些警告了.
參考資料:
Suppress warning “Category is implementing a method which will also be implemented by its primary class”
總結
以上是生活随笔為你收集整理的警告: Category is implementing a method which will also be implemented by its primary class的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: wincap支持VC6.0的版本下载
- 下一篇: 创建服务器定时运行程序