類不能定義爲原語爲可空@IBInspectable
秒。
當您嘗試這樣做,併爲其中一個屬性值,你會在IBDesignable時間得到以下警告:
@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.
並在運行時,你會得到以下錯誤:
Failed to set (testInt) user defined inspected property on (TestDesignable): [ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key testInt.
要修復它,請將其從可選項更改爲非可選項,並將其設置爲零默認值。
無效:
@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
}
那截屏的顯示自定義類位,是該視圖或文件所有者? –
是爲視圖 – Godfather
您可能需要明確定義該變量的setter和getter方法,看看這個網站http://nshipster.com/ibinspectable-ibdesignable/ –