【问题标题】:@IBInspectable in Swift 4.0Swift 4.0 中的@IBInspectable
【发布时间】:2018-03-17 23:05:01
【问题描述】:

在迁移到 Swift 4.0 时,我遇到了 @IBInspectable 的问题,

open class SVContactBubbleView: UIView 
{
   @IBInspectable open var dataSource: SVContactBubbleDataSource? //ERROR..!!
   @IBInspectable open var delegate: SVContactBubbleDelegate? //ERROR..!!
}

public protocol SVContactBubbleDataSource
{
    //Methods here
}

public protocol SVContactBubbleDelegate
{
    //Methods here
}

出现的错误是:

属性不能被标记为@IBInspectable,因为它的类型不能 用Objective-C表示

Swift 3 中,它运行良好。我不明白Swift 4 出了什么问题。

此外,编译器没有显示任何建议。它只是显示一条错误消息。

【问题讨论】:

  • 错误确实很明显。 In 真的不能在 Swift 3 中工作,即使它通过了编译。在 Swift 3 中,界面生成器中的编辑器是如何找到你的?
  • 协议是class 协议吗?并标记为@objc?另请注意,代表可能应该是weak
  • 如果你想在 IB 中连接它们,它们应该是 @IBOutlets,而不是 @IBInspectable

标签: ios delegates datasource swift4 ibinspectable


【解决方案1】:

在 Swift 4 中为委托和数据源添加符号 @objc(如下代码所示)

open class SVContactBubbleView: UIView {
    @IBInspectable open var dataSource: SVContactBubbleDataSource? 
    @IBInspectable open var delegate: SVContactBubbleDelegate? 
}

@objc  // add notation here
public protocol SVContactBubbleDataSource
{
    //Methods here
}


@objc  // add notation here
public protocol SVContactBubbleDelegate
{
    //Methods here
}


这是参考。带有错误解决方案的快照:

这是@objc 表示法的 Apple 文档 - Protocol - Optional Protocol Requirements

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-24
    • 1970-01-01
    相关资源
    最近更新 更多