Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
496 views
in Technique[技术] by (71.8m points)

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...