简体   繁体   English

Swift:计算属性的默认初始化程序

[英]Swift: Default Initializer for Computed Property

It seems like I can't give a computed property a default value using the Default Initializers method that can be used for Stored Properties. 我似乎无法使用可用于存储属性的Default Initializers方法为计算属性提供默认值。 Here's a use case where I would need this: 这是一个用例,我需要这个:

@IBDesignable public class MyRoundedCornersView: UIView {

    @IBInspectable var cornerRadius: CGFloat {
        get {
            return self.layer.cornerRadius
        }
        set {
            self.layer.cornerRadius = newValue
        }
    }

}

I'd like to give the cornerRadius a default value. 我想给cornerRadius一个默认值。 But the following doesn't work (just added = 8.0 ): 但以下不起作用(刚添加= 8.0 ):

@IBDesignable public class MyRoundedCornersView: UIView {

    @IBInspectable var cornerRadius: CGFloat = 8.0 {
        get {
            return self.layer.cornerRadius
        }
        set {
            self.layer.cornerRadius = newValue
        }
    }

}

I get the not so useful '(() -> () -> $T1) -> $T2' is not identical to 'Double' error. 我得到了不太有用的'(() -> () -> $T1) -> $T2' is not identical to 'Double'错误不同。 I looked up here to find out how to do this but it seems Apple doesn't support default values for computed properties. 我在这里查找了如何做到这一点,但似乎Apple不支持计算属性的默认值。 At least I couldn't find anything. 至少我找不到任何东西。

So how can I still make sure that I have a default value for my property? 那么我怎样才能确保我的属性有默认值? This thread says it isn't possible within the init method either – but even if, I'd like to keep the default value near my property definition. 这个线程说在init方法中也不可能 - 但即使我想在我的属性定义附近保留默认值。 Does anyone have an idea? 有没有人有想法?

It doesn't make sense to set a default value to a computed property because it doesn't have a proper value : 将默认值设置为计算属性没有意义,因为它没有正确的值:

[...] computed properties, which do not actually store a value. [...]计算属性,实际上不存储值。 Instead, they provide a getter and an optional setter to retrieve and set other properties and values indirectly. 相反,它们提供了一个getter和一个可选的setter来间接检索和设置其他属性和值。

Maybe you want to set the default in the layer type, like : 也许你想在图层类型中设置默认值,例如:

class Layer { // <- The type of layer
    var cornerRadius: CGFloat = 8.0
    // ...
}

Another solution is to use a stored property like this : 另一个解决方案是使用这样的存储属性:

var cornerRadius:CGFloat = 8.0
{
    didSet
    {
        self.layer.cornerRadius = self.cornerRadius
    }
}

Edit: This does not guarantee that layer.cornerRadius == self.cornerRadius, especially if the layer.cornerRadius is set directly 编辑:这不保证layer.cornerRadius == self.cornerRadius,特别是如果直接设置了layer.cornerRadius

It's possible to init the computed property in init , like this. 这是可能的初始化计算的财产init ,像这样。

class TestProperty {
    var b = 0
    var a: Int {
        get {
            return b + 1
        }
        set {
            b = newValue - 1
        }
    }

    init(a: Int) {
        self.a = a
    }
}

But one can just provide a default value for the stored property b , and retrieve a from b . 但是,一个可以只提供存储属性的默认值b ,并获取ab

The reason why computed property exists is that you can calculate(or update) something based on other properties, and directly use it through computed property. 存在计算属性的原因是您可以基于其他属性计算(或更新)某些内容,并直接通过计算属性使用它。

I wonder why you would like to provide a default value for computed properties. 我想知道你为什么要为计算属性提供默认值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM