11

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.

enter image description here

And this is my class enter image description here

Godfather
  • 4,040
  • 6
  • 43
  • 70
  • That screen shot that shows the custom class bit, is that for the view or for the file owner? – The Beanstalk Sep 09 '15 at 17:06
  • 2
    You might need to explicitly define the setter and getter methods for the variable, check out this site http://nshipster.com/ibinspectable-ibdesignable/ – The Beanstalk Sep 09 '15 at 17:09
  • Seems if i leave a default value without a setter/getter also works, so i'll keep in this way! So basically @IBInspectable var cellWidth: CGFloat = 0 instead of an optional. – Godfather Sep 09 '15 at 17:12
  • Might also try `@IBInspectable var cellWidth: CGFloat!`, but don't fix it if it isn't broke. – The Beanstalk Sep 09 '15 at 17:14

1 Answers1

10

You cannot define primitives as nullable for @IBInspectables.

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
}
Senseful
  • 86,719
  • 67
  • 308
  • 465
  • Why `@IBInspectable var testColor: UIColor? = nil` can be nil? If at the beginning you said: "You cannot define primitives as nullable for @IBInspectables." – Santiago Carmona González Apr 02 '17 at 19:12
  • 2
    @Santiagocarmonagonzález People usually denote "value types" as primitives. `UIColor` is a reference type, it can be `nil`. In short, designables must be representable in Obj-C and in Obj-C only reference types can be `nil`. – Sulthan Apr 02 '17 at 19:16
  • 2
    I would give you 100-million points for making this super-blunt for me. I've encountered this "bug" in almost every project I've undertaken since swift came out. thanks! – atlex2 Jul 15 '17 at 14:01
  • I am getting the `IBDesignable time` warning even after intitializing the primitive types. – Anjan Biswas Aug 10 '17 at 00:08