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

前端 未结 3 1037
醉梦人生
醉梦人生 2021-01-01 16:15

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

    @IBDesignable public class CircularIndicator: UIView {
          


        
3条回答
  •  自闭症患者
    2021-01-01 16:48

    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.

提交回复
热议问题