【问题标题】:Implement a protocol in Swift [duplicate]在 Swift 中实现一个协议 [重复]
【发布时间】:2014-06-13 08:16:17
【问题描述】:

我正在使用一个 Objective-C 库。它有一个协议

@protocol RNFrostedSidebarDelegate <NSObject>
@optional
- (void)sidebar:(RNFrostedSidebar *)sidebar didTapItemAtIndex:(NSUInteger)index;
- (void)sidebar:(RNFrostedSidebar *)sidebar didEnable:(BOOL)itemEnabled itemAtIndex:(NSUInteger)index;
@end

我正在尝试在 Swift 中实现它。这个func应该怎么写?

【问题讨论】:

标签: swift


【解决方案1】:

这将是一个实现协议的 Swift 类

class MyForstedSidebarDelegate : RNFrostedSidebarDelegate {

    func sidebar(sidebar: RNFrostedSidebar, didTapItemAtIndex index: UInt) {
        ... do stuff ...
    }

    func sidebar(sidebar: RNFrostedSidebar, didEnable itemEnabled: Bool, itemAtIndex index: UInt) {
        ... do stuff ...
    }

}

【讨论】:

  • 效果很好。谢谢!
【解决方案2】:

这样的东西可能与您最初的概念相似:

@objc protocol RNFrostedSidebarDelegate {
    @optional func sideBarDidTapItem(sidebar: RNFrostedSidebar, index: UInt) -> ()
    @optional func sideBarDidEnable(sidebar: RNFrostedSidebar, itemEnabled: Bool, index: UInt) -> ()
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-08
    • 2015-11-02
    • 2015-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多