此类与@IBInspectable不符合键值编码 [英] This class is not key value coding-compliant with @IBInspectable
问题描述
我在自定义视图上创建了一个@ IBDesignable ,以便从IB设置属性。但是我觉得这个课程不符合关键值编码,尽管我班上有这个属性。
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?
}
无法设置(cellWidth)用户定义的检查属性(Test.ExclusiveSelectorView) :[setValue:forUndefinedKey:]:此类不是密钥cellWidth的密钥值编码兼容。
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.
这是我的班级
< img src =https://i.stack.imgur.com/cVRPW.pngalt =在此输入图像说明>
推荐答案
您不能将基元定义为 @IBInspectable
的可为空。
You cannot define primitives as nullable for @IBInspectable
s.
当您尝试时要这样做,并设置其中一个属性的值,您将在IBDesignable时间收到以下警告:
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:忽略用户定义的运行时attri在TestDesignable实例上为关键路径testInt提供静音。尝试设置其值时遇到异常:[setValue:forUndefinedKey:]:此类不是密钥testInt的密钥值编码兼容。
@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:
无法设置(testInt)用户定义的检查属性( TestDesignable):[setValue:forUndefinedKey:]:此类不是密钥testInt的密钥值编码兼容。
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.
无效:
@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
}
有效期:
@IBDesignable class TestDesignable: UIView {
@IBInspectable var testInt: Int = 0
@IBInspectable var testFloat: CGFloat = 0
@IBInspectable var testPoint: CGPoint = .zero
@IBInspectable var testColor: UIColor? = nil
}
这篇关于此类与@IBInspectable不符合键值编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!