Extensions May not contain Stored properties

69,703

Solution 1

You can override the setter/getter so that it isn't a stored property and just forwards the set/get to the layer.

extension UIButton {
    @IBInspectable var borderWidth : CGFloat {
        set {
            layer.borderWidth = newValue
        }

        get {
            return layer.borderWidth
        }
    }
}

Solution 2

Extensions cannot add stored properties. From the docs (Computed Properties section):

Note

Extensions can add new computed properties, but they cannot add stored properties, or add property observers to existing properties.

If you have a need for stored properties, you should create a subclass, like so:

class CustomButton : UIButton
{
    @IBInspectable var borderWidth : CGFloat
        {
        didSet{
            layer.borderWidth = borderWidth
        }
    }

}

Solution 3

In Swift, I'm importing a static library written in Objective-C. The protocol below, in that library, has a method and a property.

@class Message;

@protocol LocalService 

@property (readonly) int action;

- (Message *)getResponse:(Message *)request;

@end

Trying to have a class conform to that protocol, delivers the messages below:

1-) Type 'ShowInitialViewLocalService' does not conform to protocol 'LocalService

2-) Extensions may not contain stored properties

The code provided below, fixes this issue:

import UIKit

class ShowInitialViewLocalService: NSObject{

}

extension ShowInitialViewLocalService : LocalService {
    
    var action: Int32 {
        get { return View.InitialView.rawValue }
    }
    
    func getResponse(_ request: Message) -> Message {
        let response = Response(source: request.target, target: request.source, relatedView: View.InitialView.rawValue, action: request.action, data: request.data)
        return response
    }
}

I hope this will help someone.

Share:
69,703

Related videos on Youtube

Mohamed DiaaEldin
Author by

Mohamed DiaaEldin

Writing code 1/3 of my life on earth so far.

Updated on July 09, 2022

Comments

  • Mohamed DiaaEldin
    Mohamed DiaaEldin almost 2 years

    Can I implement this in Swift with Extensions without the need to inheritance?. I get this error Extensions May not contain Stored properties

    extension UIButton
    {
        @IBInspectable var borderWidth : CGFloat
            {
            didSet{
                layer.borderWidth = borderWidth
            }
        }
    
    }
    
  • Mohamed DiaaEldin
    Mohamed DiaaEldin about 8 years
    I don't want to store variable I want to change layer.borderWidth from interface builder
  • Alexander
    Alexander about 8 years
    dan's answer might work for you then, though my concern is that the UIButton might not be fully initialized before the getter/setter is invoked.
  • Rishabh Tayal
    Rishabh Tayal over 7 years
    This technic works but it doesn't update the UI in the xib/storyboard.
  • Nikhil Manapure
    Nikhil Manapure almost 7 years
    Do setNeedsLayout() after setting.