I created a @IBDesignable on a custom view to set a property from IB. But i get this class is not key value coding-compliant despite i have that attribute in my class.
@IBDesignable class ExclusiveSelectorView: UIView {
@IBInspectable var cellWidth: CGFloat?
}
Failed to set (cellWidth) user defined inspected property on (Test.ExclusiveSelectorView): [ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key cellWidth.
And this is my class
You cannot define primitives as nullable for @IBInspectable
s.
When you attempt to do so, and set the value for one of these properties, you'll get the following warning at IBDesignable time:
@IBDesignable class TestDesignable IB Designables: Ignoring user defined runtime attribute for key path "testInt" on instance of "TestDesignable". Hit an exception when attempting to set its value: [ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key testInt.
And at runtime, you'll get the following error:
Failed to set (testInt) user defined inspected property on (TestDesignable): [ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key testInt.
To fix it, change it from an optional to non-optional, and give it some zero default value.
Invalid:
@IBDesignable class TestDesignable: UIView {
@IBInspectable var testInt: Int? = nil // crash
@IBInspectable var testFloat: CGFloat? = nil // crash
@IBInspectable var testPoint: CGPoint? = nil // crash
@IBInspectable var testColor: UIColor? = nil
}
Valid:
@IBDesignable class TestDesignable: UIView {
@IBInspectable var testInt: Int = 0
@IBInspectable var testFloat: CGFloat = 0
@IBInspectable var testPoint: CGPoint = .zero
@IBInspectable var testColor: UIColor? = nil
}
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