objective-c - 将 @objc 添加到 Swift 方法或变量会产生不良影响吗?

标签 objective-c swift

我在 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/

相关文章:

ios - 倒计时器如何在后台工作

ios - 如何使用 Swifter 授权 Twitter

ios - Swift:可以从许多地方访问的 ViewController 模式

swift - 无法在 map View 中快速移动

ios - ObjC : memory usage of delegate vs block?

iphone - 如何使用 iOS 5.1 打开首选项/设置?

ios - 单击时将 TableView 单元格编号传递到另一个 View ,一开始始终为 null

ios - 根据 UILabel 字体大小调整 CGSize

ios - 代码=134110 - 强制目标属性上缺少属性值的验证错误

ios - 仅在 iPad 2 型号上无法检测到 wifi 打开或关闭