ios - 如何在具有相同协议(protocol)变量的 Swift 中使用多个协议(protocol)?

标签 ios swift protocols

我正在快速实现两个协议(protocol),GADCustomEventInterstitialGADCustomEventBanner

这两个协议(protocol)都需要一个名为delegate 的属性。 delegate在每个协议(protocol)中都是不同的类型,因此会产生冲突。

 class ChartBoostAdapter : NSObject, GADCustomEventInterstitial, GADCustomEventBanner, ChartboostDelegate{
        var delegate:GADCustomEventInterstitialDelegate?; // Name conflict
        var delegate:GADCustomEventBannerDelegate?; // Name conflict
         override init(){

        }
    ...

    }

最佳答案

They are libraries/frameworks it's not my definition

那么显然你不能让同一个类同时采用这两种协议(protocol)。但你真的不需要。只需将此功能分成两个不同的类,这显然是这些协议(protocol)的设计者的意图。您应该有一个采用 GADCustomEventInterstitial 并具有它的 delegate 的类,以及另一个 采用GADCustomEventBanner 和有它的 委托(delegate)。你有什么理由试图强制这些成为同一个类(class)?与您使用框架的所有事情一样,不要对抗框架,服从它。

关于ios - 如何在具有相同协议(protocol)变量的 Swift 中使用多个协议(protocol)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29585572/

相关文章:

android - Android 的 Pan 手势识别器?

ios - 触摸时取消 UIButton 触摸

swift - 更改 Firebase 观察器 SWIFT 4 后函数返回

html - URL 中 http 协议(protocol)声明中的正斜杠

objective-c - UIScrollView 的最大缩放比例

ios - UISearchBar在文本中带有背景

swift 协议(protocol)扩展默认实现与类中的实际实现

ios - 以编程方式创建形状 View 并在 Collection View 单元格上使用

protocols - python dpkt pcap 如何获取协议(protocol)?

swift - 我们可以为协议(protocol)扩展的方法参数设置默认值吗?如果是这样,我们可以覆盖实现该协议(protocol)的结构上的那些方法吗?