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. 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. But the following doesn't work (just added = 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. I looked up here to find out how to do this but it seems Apple doesn't support default values for computed properties. 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. Does anyone have an idea?
Computed properties are part of a family of property types in Swift. Stored properties are the most common which save and return a stored value whereas computed ones are a bit different. A computed property, it's all in the name, computes its property upon request.
Swift provides a default initializer for any structure or class that provides default values for all of its properties and doesn't provide at least one initializer itself. The default initializer simply creates a new instance with all of its properties set to their default values.
Swift Computed Property For example, class Calculator { // define stored property var num1: Int = 0 ... } Here, num1 is a stored property, which stores some value for an instance of Calculator . Here, sum is a computed property that doesn't store a value, rather it computes the addition of two values.
Defining throwing properties in Swift A computed property exists as an accessor to data representing the underlying sample file. As the Data initializer can throw an error, we benefit from defining our computed property as throwing.
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.
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
It's possible to init the computed property in init
, like this.
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
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With