Xcode 8 Objective-C 类别警告
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39665979/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Xcode 8 Objective-C category warning
提问by jherg
I'm using Xcode 8 and Swift 3.0. What does this error message mean?
我正在使用 Xcode 8 和 Swift 3.0。这个错误信息是什么意思?
ld: warning: Some object files have incompatible Objective-C category definitions. Some category metadata may be lost. All files containing Objective-C categories should be built using the same compiler.
ld:警告:某些目标文件具有不兼容的 Objective-C 类别定义。某些类别元数据可能会丢失。所有包含 Objective-C 类别的文件都应该使用相同的编译器构建。
回答by juanjo
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
:
我在UIColor
扩展中也遇到了这个问题,我的应用程序完全是用 swift 制作的,除了一些使用 Objective-c 的框架,所以我在声明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...
nonobjc 属性告诉编译器使声明在 Objective-C 代码中不可用...
Since this code is unavailable to Objective-C the warning disappears.
由于此代码对 Objective-C 不可用,警告消失了。
回答by Hejazi
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
):
不确定这背后的确切原因是什么,但为了摆脱警告,我必须将计算类型属性 ( class var
) 转换为类型方法 ( class func
):
extension NSParagraphStyle {
class func defaultStyle() -> NSParagraphStyle {
return ...
}
}
回答by Groot
This warning appeared in my project after adding a framework that used Objective-C in my application that otherwise used Swift 3 entirely.
在我的应用程序中添加了一个使用 Objective-C 的框架后,这个警告出现在我的项目中,否则完全使用 Swift 3。
By declaring all static functions and static variablesin all extensionsas @nonobjc
this warning went away.
通过声明所有的静态函数和静态变量在所有分机作为@nonobjc
该警告就走开了。
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)
}
回答by Renato Ioshida
Google Analyticspod
谷歌分析吊舱
In Build Settings -> Other Linker Flagsif you have the -ObjCon -l"GoogleAnalytics"flag this warning will appear. I don`t know why or how to resolve, but can be your problem too.
在Build Settings -> Other Linker Flags 中,如果您在-l"GoogleAnalytics"标志上设置了-ObjC,则会出现此警告。我不知道为什么或如何解决,但也可能是您的问题。
回答by JAL
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.
对我来说,问题是我在使用 Xcode 8 构建的 Swift 3 应用程序中使用了来自供应商的第三方框架,该框架是使用 Xcode 7 构建的。使用最新版本的 Xcode 构建的新框架。
回答by Bogdan
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.
添加@nonobjc 有帮助。
回答by A.Kant
I was able to solve my problem when I changed the "class var" to "class func":
当我将“ class var”更改为“ class func”时,我能够解决我的问题:
There was:
有:
class var applicationVersionNumber: String {
if let version = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String {
return version
}
return "Version Number Not Available"
}
Has become:
已经成为:
class func applicationVersionNumber() -> String {
if let version = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String {
return version
}
return "Version Number Not Available"
}
Source: https://forums.developer.apple.com/message/146579#146579
来源:https: //forums.developer.apple.com/message/146579#146579
回答by Adam Sharp
Rather than marking each member as @nonobjc
individually, you can instead mark the entire extension as @nonobjc
:
而不是作为标记每个成员@nonobjc
单独,可以改为标记整个扩展为@nonobjc
:
@nonobjc extension UIStoryboard {
static let main = UIStoryboard(name: "Main", bundle: nil)
static let welcome = UIStoryboard(name: "Main", bundle: nil)
}