Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

This class is not key value coding-compliant with @IBInspectable

Tags:

ios

swift

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

like image 434
Godfather Avatar asked Sep 09 '15 17:09

Godfather


1 Answers

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
}
like image 189
Senseful Avatar answered Oct 03 '22 23:10

Senseful