Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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?

like image 279
Jeehut Avatar asked Aug 21 '14 11:08

Jeehut


People also ask

How does Swift define computed property?

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.

What is a property initializer Swift?

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.

How do I create a computed property in Swift?

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.

Can a computed property throw Swift?

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.


2 Answers

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

like image 54
Axel Guilmin Avatar answered Nov 15 '22 08:11

Axel Guilmin


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.

like image 29
Windor C Avatar answered Nov 15 '22 06:11

Windor C