Inheritable @IBInspectable var in Swift?

Suppose I create a variable @IBInspectablein a base class@IBDesignable .

Now the attribute does not make the property checked in the derived class. It is clear in design; you want to create a derived class that sets the base attributes in accordance with its own logic and prevents the user from touching them.

However, suppose I want the same attributes to be checked in a derived class . The problem is that it is @IBInspectableused only with variable definitions, so I need to redefine the variable even if the definition is already provided (that when I skipped Objective-C: you can change the class interface without touching if you knew what you were doing).

This has the following drawback: you cannot override the base stored variable, and you need to provide an explicit getter and setter. Therefore, the cleanest code I came up with is this:

// derived class
@IBInspectable override var borderWidth: CGFloat {
get {
    return super.borderWidth
}
set {
    super.borderWidth = newValue
}
}

// base class
@IBInspectable var borderWidth: CGFloat {
set {
    layer.borderWidth = newValue
    // this can be a very complicated setter, of course
}
get {
    return layer.borderWidth
    // this can be a very complicated getter, of course
}
}

Question: is there a shorthand syntax?

(If not, and if other people like the idea, I will probably send a radar to Apple wizards.)

+4
source share
1 answer

Note: this has been fixed with Xcode 6 beta 3: Interface Builder also sees controlled properties in the superclass

This topic has a question in the Apple forums.

For Xcode 6 beta 2 and below: reduce the code using the didSetfollowing and it should work:

// derived class
@IBInspectable override var borderWidth: CGFloat { didSet{} }

// base class
@IBInspectable var borderWidth: CGFloat {
set {
    layer.borderWidth = newValue
    // this can be a very complicated setter, of course
}
get {
    return layer.borderWidth
    // this can be a very complicated getter, of course
}
}
0
source

Source: https://habr.com/ru/post/1546730/


All Articles