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

标签 swift xcode swift4 xcode9-beta

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

有没有办法摆脱它?

最佳答案

也许。

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

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

我通过以下更改解决了这个问题:

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

收件人:

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

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

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

关于swift - 如何修复 "' @IBInspectable' 属性对无法在 Objective-C 中表示的属性毫无意义“警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46024160/

相关文章:

image-processing - 使用 Swift 4 在 Metal Compute Kernel 中传递参数

ios - 如何使用 swift 4 通过 Image Picker Controller 发送图像数据

swift - 使用 "Unknown Command Type <NSBatchDeleteRequest,..>"执行 NSBatchDeleteRequest 时应用程序崩溃

ios - 添加具有比例宽度的 subview 到 ScrollView

ios - viewDidload 和 viewDidAppear 没有被调用?

iphone - 局部声明隐藏实例变量 - tableView 未合成

ios - 在 Web View 中点击应用程序 url 方案后如何返回应用程序

xcode - Swift 博客或播客

swift - CIFaceFeature 只检测一张脸

swift - 如何在 Swift 中限制用户名的字符