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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46024160/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 10:12:33  来源:igfitidea点击:

How to fix "'@IBInspectable' attribute is meaningless on a property that cannot be represented in Objective-C" warning

swiftxcodeswift4xcode9-beta

提问by Adrian

In Xcode 9 and Swift 4 I always get this warning for some IBInspectableproperties:

在 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
        }
    }
}

Is there a way to get rid of it ?

有没有办法摆脱它?

回答by dfd

Maybe.

也许。

The exact error(not warning) I got when doing a copy/paste of class CircularIndicator: UIViewis:

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

Property cannot be marked @IBInspectable because its type cannot be represented in Objective-C

属性不能被标记为@IBInspectable 因为它的类型不能在 Objective-C 中表示

I resolved it by making this change:

我通过进行此更改解决了它:

@IBInspectable var backgroundIndicatorLineWidth: CGFloat? {  // <-- warning here
    didSet {
        backgroundIndicator.lineWidth = backgroundIndicatorLineWidth!
    }
}

To:

到:

@IBInspectable var backgroundIndicatorLineWidth: CGFloat = 0.0 {
    didSet {
        backgroundIndicator.lineWidth = backgroundIndicatorLineWidth!
    }
}

Of course, backgroundIndicatoris undefined in my project.

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

But if you are coding against didSet, it looks like you just need to define a default value instead of making backgroundIndicatorLineWidthoptional.

但是,如果您针对 进行编码didSet,则看起来您只需要定义一个默认值而不是使其成为backgroundIndicatorLineWidth可选值。

回答by Aaban Tariq Murtaza

Below two points might helps you

以下两点或许能帮到你

  1. As there is no concept of optional in objective c, So optional IBInspectable produces this error. I removed the optional and provided a default value.

  2. If you are using some enumerations types, then write @objc before that enum to remove this error.

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

  2. 如果您正在使用某些枚举类型,则在该枚举之前写入 @objc 以消除此错误。

回答by Shakeel Ahmed

Swift - 5

斯威夫特 - 5

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

To

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