扩展名可能不包含存储的属性 [英] Extensions May not contain Stored properties

查看:588
本文介绍了扩展名可能不包含存储的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在Swift with Extensions中实现它而不需要继承吗?
我收到此错误
扩展程序不能包含已存储的属性

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

}


推荐答案

你可以覆盖setter / getter,这样就不会存储的属性,只是将set / get转发给图层。

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

这篇关于扩展名可能不包含存储的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆