对于无法在Objective-C中表示的属性,如何修复“'@IBInspectable'"属性毫无意义.警告 [英] How to fix "'@IBInspectable' attribute is meaningless on a property that cannot be represented in Objective-C" warning

查看:353
本文介绍了对于无法在Objective-C中表示的属性,如何修复“'@IBInspectable'"属性毫无意义.警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆