Created
July 2, 2015 10:50
-
-
Save sveinhal/49bd95d11fb5ac8dcd26 to your computer and use it in GitHub Desktop.
How to implement an Obj-C protocol with custom getter name in Swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SomeClass: SomeProtocol { | |
private var _enabled: Bool = false | |
var enabled: Bool { | |
@objc(isEnabled) get { return _enabled } | |
set { _enabled = newValue } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@protocol SomeProtocol <NSObject> | |
@property (nonatomic, getter=isEnabled) BOOL enabled; | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can expose a Swift property or function to the Objective-C runtime with a different name, using the
@objc(name)
attribute. However, exposing a different name for only the getter, requires that you explicitly define a setter and getter individually, which again requires that you use another (private) variable for backing.