【问题标题】:Property @IBInspectable cannot be marked属性@IBInspectable 无法标记
【发布时间】:2018-02-08 03:59:54
【问题描述】:

如何解决这个问题,安装 xcode 9 后告诉我这个

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

/// The mode of the gradient. The default is `.Linear`.
@IBInspectable open var mode: Mode = .linear {
    didSet {
        setNeedsDisplay()
    }
}

/// The direction of the gradient. Only valid for the `Mode.Linear` mode. The default is `.Vertical`.
@IBInspectable open var direction: Direction = .vertical {
    didSet {
        setNeedsDisplay()
    }
}

【问题讨论】:

  • 错误信息很清楚
  • 请编辑您的问题以包含使用正确代码格式格式化为文本的代码。切勿在问题中包含代码作为屏幕截图。
  • 对不起,我编辑了。
  • 在枚举前面添加@objc 会消除警告,但不会使属性显示在属性检查器中或身份检查器中的用户定义的运行时属性中。相反,我遵循了stackoverflow.com/questions/27432736/… 来帮助解决问题。这在 Swift 4.0/xcode 9 中仍然是一个问题。我还添加了我的支持变量的答案(如果需要在类中进行其他计算)。

标签: swift swift4


【解决方案1】:

你需要为枚举添加@objc:

@objc public enum Mode: Int {
        ...
}

@objc public enum Direction: Int {
        ...
}

您确实需要 Int 基本类型,如果它是 @objc 样式,则不能是“空白”。

【讨论】:

  • 但是 var 属性 'mode' (类型为 'objc enum Mode' )仍然没有出现在“Interface Builder”的“Attributes inspector”中。
  • 因为 IB 即使是 ObjC 也不支持枚举 :(
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-27
相关资源
最近更新 更多