日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

警告: Category is implementing a method which will also be implemented by its primary class

發布時間:2023/12/18 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 警告: 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類的方法時可能會導致問題。

解決方案

  • 用繼承的方式重寫父類方法
  • 用類目重寫原類的方法, 需要通過runtime的method swizzling來進行方法IMP的交換處理.
  • 忽略警告處理
    1>代碼方式
  • #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wobjc-protocol-method-implementation" // your override #pragma clang diagnostic pop

    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的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。