带有可选功能实现的Swift @objc协议会导致构建错误

时间:2016-11-15 19:18:00

标签: ios objective-c swift xcode swift-protocols

我有以下协议(我将其用作故事板的IBOutlet参考,因此它必须与@objc一起使用。)

/// This protocol implemented by views that contain a **main** 
    collection view.
@objc protocol CollectionInViewMethods {

      /// Notify the view that scrolling occurred.
      @objc optional func setSortingMenuFrame(newFrame: CGRect)
}

当我尝试使用swift语法访问可选功能时,如下所示:

self.cineamsView.setSortingMenuFrame?(newFrame: CGRect())

的结果
Command failed due to signal: Segmentation fault: 11

虽然强制解包构建成功(!而不是?)

有没有理由发生这种情况?我看到目标C确实支持协议中的可选功能,在我看来就像Xcode错误...

1 个答案:

答案 0 :(得分:0)

我认为你需要首先解开函数,看看它在调用之前是不是nil(只有当你用objective-c协议桥接它时)

此博客解释说: http://useyourloaf.com/blog/swift-optional-protocol-methods/