我在 Swift 项目中导入了一些 Objective-C 文件,并尝试访问一些 Swift 类。
根据Apple's Guideline ,我将 @objc 添加到我希望将其公开给 Objective-C 文件的方法中。
但问题是,这个“@objc”对我的 Swift 项目有副作用吗?
以下代码是单例本地数据管理器。
@objc class LocalDataManager {
@objc public static let shared = LocalDataManager()
private init() {}
@objc var nickName: String {
get { return loadData("nickName") } // loadData is a convenience access method to UserDefaults
set { UserDefaults.standard.set(newValue, forKey: "nickName") }
}
}
最佳答案
@objc
关键字确实会严重影响性能。 Apple docs状态:
applying the
@objc
attribute can increase the compiled size of an app and adversely affect performance.
因此,仅当您确实需要时才使用 @objc
。
关于objective-c - 将 @objc 添加到 Swift 方法或变量会产生不良影响吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48246124/