如何修复"'@IBInspectable'属性对于无法在Objective-C中表示的属性无意义"警告

Adr*_*ian 17 xcode swift swift4 xcode9-beta

在Xcode 9和Swift 4中,我总是会收到一些IBInspectable属性的警告:

    @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
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

有办法摆脱它吗?

dfd*_*dfd 27

也许.

我在复制/粘贴类时得到的确切错误(不是警告)CircularIndicator: UIView是:

属性不能标记为@IBInspectable,因为其类型无法在Objective-C中表示

我通过进行此更改解决了这个问题:

@IBInspectable var backgroundIndicatorLineWidth: CGFloat? {  // <-- warning here
    didSet {
        backgroundIndicator.lineWidth = backgroundIndicatorLineWidth!
    }
}
Run Code Online (Sandbox Code Playgroud)

至:

@IBInspectable var backgroundIndicatorLineWidth: CGFloat = 0.0 {
    didSet {
        backgroundIndicator.lineWidth = backgroundIndicatorLineWidth!
    }
}
Run Code Online (Sandbox Code Playgroud)

当然,backgroundIndicator在我的项目中未定义.

但是如果您正在编码didSet,看起来您只需要定义默认值而不是backgroundIndicatorLineWidth可选.


Aab*_*aza 7

以下两点可能对您有所帮助

  1. 由于目标c中没有可选概念,因此可选的IBInspectable会产生此错误.我删除了可选项并提供了默认值.

  2. 如果您使用的是某些枚举类型,请在该枚举之前编写@objc以删除此错误.


Sha*_*med 5

斯威夫特 - 5

//Change this with below
@IBInspectable public var shadowPathRect: CGRect!{
    didSet {
        if shadowPathRect != oldValue {
            setNeedsDisplay()
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

@IBInspectable public var shadowPathRect: CGRect = CGRect(x:0, y:0, width:0, height:0) {
    didSet {
        if shadowPathRect != oldValue {
            setNeedsDisplay()
        }
    }
}
Run Code Online (Sandbox Code Playgroud)