对于无法在Objective-C中表示的属性,如何修复“'@IBInspectable'"属性毫无意义.警告 [英] How to fix "'@IBInspectable' attribute is meaningless on a property that cannot be represented in Objective-C" warning
问题描述
在Xcode 9和Swift 4中,对于某些IBInspectable
属性,我总是会收到此警告:
In Xcode 9 and Swift 4 I always get this warning for some IBInspectable
properties:
@IBDesignable public class CircularIndicator: UIView {
// this has a warning
@IBInspectable var backgroundIndicatorLineWidth: CGFloat? { // <-- warning here
didSet {
backgroundIndicator.lineWidth = backgroundIndicatorLineWidth!
}
}
// this doesn't have a warning
@IBInspectable var topIndicatorFillColor: UIColor? {
didSet {
topIndicator.fillColor = topIndicatorFillColor?.cgColor
}
}
}
有办法摆脱它吗?
推荐答案
也许.
在复制/粘贴类CircularIndicator: UIView
时得到的确切的错误(不是警告)是:
The exact error (not warning) I got when doing a copy/paste of class CircularIndicator: UIView
is:
无法将属性标记为@IBInspectable,因为其类型不能为 代表在Objective-C
Property cannot be marked @IBInspectable because its type cannot be represented in Objective-C
我通过进行以下更改来解决了该问题:
I resolved it by making this change:
@IBInspectable var backgroundIndicatorLineWidth: CGFloat? { // <-- warning here
didSet {
backgroundIndicator.lineWidth = backgroundIndicatorLineWidth!
}
}
收件人:
@IBInspectable var backgroundIndicatorLineWidth: CGFloat = 0.0 {
didSet {
backgroundIndicator.lineWidth = backgroundIndicatorLineWidth!
}
}
当然,backgroundIndicator
在我的项目中是未定义的.
Of course, backgroundIndicator
is undefined in my project.
但是,如果您要针对didSet
进行编码,则看起来您只需要定义一个默认值即可,而不必将backgroundIndicatorLineWidth
设置为可选.
But if you are coding against didSet
, it looks like you just need to define a default value instead of making backgroundIndicatorLineWidth
optional.
这篇关于对于无法在Objective-C中表示的属性,如何修复“'@IBInspectable'"属性毫无意义.警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!