Xcode 8 Objective-C category warning

后端 未结 8 1182
生来不讨喜
生来不讨喜 2020-12-05 09:13

I\'m using Xcode 8 and Swift 3.0. What does this error message mean?

ld: warning: Some object files have incompatible Objective-C category definition

相关标签:
8条回答
  • 2020-12-05 09:32

    This warning appeared in my project after adding a framework that used Objective-C in my application that otherwise used Swift 3 entirely.

    By declaring all static functions and static variables in all extensions as @nonobjc this warning went away.

    For example

    extension Notification.Name {
        @nonobjc static let MyNotificationName = Notification.Name("NNSongFavoriteStatusDidChangeNotification")
    }
    

    or

    extension UIColor {
        @nonobjc static let superGiantRed = UIColor(red: 180.0/255.0, green: 40.0/255.0, blue: 27.0/255.0, alpha: 1.0)
    }
    
    0 讨论(0)
  • 2020-12-05 09:36

    In my case it was a class variable.

    public extension NSObject {
        public class var nameOfClass: String{
            return NSStringFromClass(self).components(separatedBy: ".").last!
        }
    

    Adding @nonobjc helped.

    0 讨论(0)
  • 2020-12-05 09:36

    Rather than marking each member as @nonobjc individually, you can instead mark the entire extension as @nonobjc:

    @nonobjc extension UIStoryboard {
      static let main = UIStoryboard(name: "Main", bundle: nil)
      static let welcome = UIStoryboard(name: "Main", bundle: nil)
    }
    
    0 讨论(0)
  • 2020-12-05 09:37

    For me the issue was that I was using a third-party framework from a vendor built with Xcode 7 in my Swift 3 application built with Xcode 8. Because the framework was a compiled binary, the only option I had was to ask my vendor for a new framework built with the latest version of Xcode.

    0 讨论(0)
  • 2020-12-05 09:40

    I also had this issue in a UIColor extension, my app is entirely made with swift except for some frameworks that use Objective-c so I have no problem in declaring the var as @nonobjc:

    extension UIColor {
       @nonobjc static var lol: UIColor {
          return UIColor.red
       }
    }
    

    From the apple docs:

    The nonobjc attribute tells the compiler to make the declaration unavailable in Objective-C code...

    Since this code is unavailable to Objective-C the warning disappears.

    0 讨论(0)
  • 2020-12-05 09:48

    In my case, the reason was having computed type property in an extension:

    extension NSParagraphStyle {
        class var defaultStyle: NSParagraphStyle {
            return ...
        }
    }
    

    Not sure what the exact reason behind this is, but to get rid of the warning I had to convert the computed type property (class var) to a type method (class func):

    extension NSParagraphStyle {
        class func defaultStyle() -> NSParagraphStyle {
            return ...
        }
    }
    
    0 讨论(0)
提交回复
热议问题