【问题标题】:Convert typedef to Swift将 typedef 转换为 Swift
【发布时间】:2017-04-03 23:06:44
【问题描述】:

如何将此代码转换为 Swift

 typedef  UIViewController<CalendarViewControllerNavigation> CalendarViewController;

试图到处寻找解决方案,但似乎找不到对此的参考。

【问题讨论】:

  • 您在问题中使用了typealias 标签。所以你似乎已经知道答案了。
  • Swift 目前不支持表达从类继承并符合给定协议的东西的类型——但it will in a future version of the language
  • 你有什么其他方法可以做到这一点。也许创建 UIViewController 的扩展,按下协议该扩展的类型别名?

标签: objective-c swift typedef code-conversion type-alias


【解决方案1】:

在 Swift 4 中,您可以使用:

typealias CalendarViewController = UIViewController & CalendarViewControllerNavigation

使用新的protocol composition type。 另见SE-156

【讨论】:

    【解决方案2】:

    我在使用 https://github.com/jumartin/Calendar 的日历库时遇到了同样的问题。

    我已经解决了这个问题,如下所示。

    protocol CalendarViewControllerNavigation: NSObjectProtocol {
        func move(to date: Date, animated: Bool)
    }
    
    typealias CalendarTypeController = UIViewController
    
    extension CalendarTypeController:CalendarViewControllerNavigation{
        internal func move(to date: Date, animated: Bool) {
        }
        internal func deviceIsRotated() {
        }
        internal func selectedEventForAddSession(event:EventDetail) {
        }
    }
    
    
    protocol CalenderViewControllerDelegate : NSObjectProtocol {
        func calendarViewController(_ controller:CalendarTypeController, showDate:NSDate)
    }
    

    希望此代码对您有所帮助!

    【讨论】:

      猜你喜欢
      • 2017-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-02
      • 2018-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多