我有以下协议(我使用它作为对故事板的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)
}当我尝试使用快速语法访问可选函数时,如下所示:
self.cineamsView.setSortingMenuFrame?(newFrame: CGRect())它的结果是
Command failed due to signal: Segmentation fault: 11强制取消包装生成成功时(!而不是?)
有什么理由会发生这种情况吗?我看到objective C确实支持协议中的可选函数,在我看来这是一个Xcode错误……
发布于 2016-11-16 03:44:29
我认为在调用函数之前,您需要先解开函数,看看它是否不是nil (只有在使用objective-c协议桥接它的情况下)
这个博客解释了这一点:http://useyourloaf.com/blog/swift-optional-protocol-methods/
https://stackoverflow.com/questions/40617971
复制相似问题