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

谁说胖子不能爱 提交于 2019-12-18 14:17:27

问题


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

Is there a way to get rid of it ?


回答1:


Maybe.

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

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

To:

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

Of course, backgroundIndicator is undefined in my project.

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




回答2:


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.




回答3:


Swift - 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()
        }
    }
}


来源:https://stackoverflow.com/questions/46024160/how-to-fix-ibinspectable-attribute-is-meaningless-on-a-property-that-cannot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!