How to expose existing property on Obj-C class using an extension protocol in Swift

Michael Dubs

In Swift 1.1 we were able to have code like below compile and work where we exposed existing Objective-C properties through a protocol added by an extension. We also had a few where the property is handled by the extension.

@objc protocol Enableable: class {
    var enabled: Bool { get set }
}

let DisabledAlpha: CGFloat = 0.5
let EnabledAlpha: CGFloat = 1.0

extension UIButton: Enableable {}

extension UIImageView: Enableable {
    var enabled: Bool {
        get {
            return alpha > DisabledAlpha
        }
        set(enabled) {
            alpha = enabled ? EnabledAlpha : DisabledAlpha
        }
    }
}

When trying to compile this code using XCode 6.3 and Swift 1.2, we get the following error Type 'UIButton' does not conform to the protocol 'Enableable'. The UIImageView extension seems to compile fine.

Is there any way to expose these sort of existing properties from an Objective-C type or do we have to implement a proxying property with a different name?

Martin R

The compiler error message

note: Objective-C method 'isEnabled' provided by getter for 'enabled' does not match the requirement's selector ('enabled')

gives a hint about the problem. The enabled property of UIButton is inherited from UIControl and in Objective-C declared as

@property(nonatomic, getter=isEnabled) BOOL enabled

Therefore the protocol method has to be

@objc protocol Enableable: class {
    var enabled: Bool { @objc(isEnabled) get set }
}

and the implementation (similarly as in Swift 1.2 error on Objective-C protocol using getter):

extension UIImageView: Enableable {
    var enabled: Bool {
        @objc(isEnabled) get {
            return alpha > DisabledAlpha
        }
        set(enabled) {
            alpha = enabled ? EnabledAlpha : DisabledAlpha
        }
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Accessing Swift protocol property implemented in an Obj-C class from another Swift class

How to override computed property of protocol extension in Swift

Unknown swift property in obj-c class

Conforming to an Obj-C protocol's property in Swift

Can not find Swift Protocol declaration in Obj-C class

How to port a network extension NEPacketTunnelProvider class from Obj-C/Swift to Xamarin C#?

Swift property observer in protocol extension?

Swift Protocol-Oriented Programming: Can Protocol Extension Property Have Same Name As Base Class Property

Swift protocol extension with property conforming to protocol

Using an implementation of a Swift protocol within Obj-C

How to expose Foreign Key property to existing entity having navigational property using EF6 Code First

How do I expose a record property using an extension method and making the property private

Add class property to protocol in Swift

Swift Property that conforms to a Protocol and Class

How to implement objective C protocol in swift class?

A weak property in Objective-C class implementing a Swift protocol

How to initialize a class with a property that is constrained to a generic type and a protocol in Swift

Swift Class Extension Only When Conforming to Protocol

Swift extension only when conforming to Class AND protocol

How to expose a python class to c++ using cython

Swift Protocol extension: cannot assign to property: '' is a get-only property

Protocol Extension in Swift Where Object Is a Class and conforms to a Protocol

Swift Extension: augment existing class with method overloading

Swift 3 protocol extension using selector error

@obj protocol delegate method not working in second class. Swift

Swift expose the protocol to objc in protocol oriented programming

How does protocol extension work in Swift?

How to set delegate in a protocol extension for a UIkit class

Swift: static property in protocol extension CAN be overridden, but why?