JsW
JsW

Reputation: 1758

Would it have any bad influence to add @objc to a Swift method or variable?

I got some Objective-C files imported in Swift project, and tried to access some Swift classes.
According to Apple's Guideline, I added the @objc to the method I wanted it to be exposed to Objective-C files.
But the question is, does this "@objc" have any side effect to my Swift project? The following code is a singleton local data manager.

@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") }
    }
}

Upvotes: 2

Views: 426

Answers (1)

Papershine
Papershine

Reputation: 5223

The @objc keyword does severely affect performance. The Apple docs states:

applying the @objc attribute can increase the compiled size of an app and adversely affect performance.

Therefore, only use @objc if you really need too.

Upvotes: 3

Related Questions