8

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?

Community
  • 1
  • 1
Jeehut
  • 20,202
  • 8
  • 59
  • 80

2 Answers2

12

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

Axel Guilmin
  • 11,454
  • 9
  • 54
  • 64
  • I'm not sure if I understand the first solution correctly, but setting that value within the Layer class doesn't seem like the right solution here. It should be fixed within my subclass. The second approach was actually my first approach that I wanted to replace with computed properties to make sure the equality between `self.cornerRadius` and `layer.cornerRadius` is always given. – Jeehut Aug 21 '14 at 12:43
  • Yes, that what my edit is about. A solution might be to pass this default value to the layer init : init() { super.init(cornerRadius:8.0) } – Axel Guilmin Aug 21 '14 at 12:46
4

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.

Windor C
  • 1,120
  • 8
  • 17
  • 1
    Thank you for this solution! That will do it for now. But as you can see I have a use case where I would want to set a default value for the computed property in order to make sure the usage of this UIView subclass defaults to a view with rounded corners. So I was asking my self the exact opposite of what you ask me here: Why **shouldn't** there be default values for computed properties as well? Especially within subclasses where you never directly set the default value for the stored properties you're referring to in the computed one. – Jeehut Aug 21 '14 at 12:49
  • 1
    @Dschee, then you should set `self.layer.cornerRadius` to the default value you want to give, not the computed property `cornerRadius`. because computed property is not intended to do things like this, although it can do so. – Windor C Aug 22 '14 at 07:22