Extensions May not contain Stored properties

前端 未结 3 1756
后悔当初
后悔当初 2020-12-24 05:30

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

extension UIB         


        
3条回答
  •  醉话见心
    2020-12-24 05:59

    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
            }
        }
    
    }
    

提交回复
热议问题