可以;定义和使用协议

时间:2016-12-28 10:26:12

标签: ios objective-c swift prototype

我正在尝试将一些Objective-C代码迁移到我的Swift代码,但Swift中存在一些协议错误。在我的Objective-C中,一切都很完美。

我有一个课说MGCDayPlannerEKViewController,

public class MGCDayPlannerEKViewController : MGCDayPlannerViewController, UIPopoverPresentationControllerDelegate {

    public var calendar: NSCalendar!
    public var visibleCalendars: Set<NSObject>!
    public var eventStore: EKEventStore! { get }
    weak public var delegate: MGCDayPlannerEKViewControllerDelegate!

    /** designated initializer */
    public init!(eventStore: EKEventStore!)
    public func reloadEvents()
}

public protocol MGCDayPlannerEKViewControllerDelegate : NSObjectProtocol {

    @available(iOS 4.0, *)
    optional public func dayPlannerEKEViewController(vc: MGCDayPlannerEKViewController!, willPresentEventViewController eventViewController: EKEventViewController!)
    @available(iOS 4.0, *)
    optional public func dayPlannerEKViewController(vc: MGCDayPlannerEKViewController!, navigationControllerForPresentingEventViewController eventViewController: EKEventViewController!) -> UINavigationController!
}

在我的Swift类中,我继承了这个类,并在我的新类

中创建了一个协议
protocol WeekViewControllerDelegate :MGCDayPlannerEKViewControllerDelegate,CalendarViewControllerDelegate,UIViewControllerTransitioningDelegate {

}
class WeekViewController: MGCDayPlannerEKViewController {

    var delegate: WeekViewControllerDelegate?
    var showDimmedTimeRanges = false
    var isiPad : Bool {
        if UIDevice.currentDevice().userInterfaceIdiom == .Pad {
            return true
        }else{
            return false
        }
    }
}

但我在委托声明中遇到错误。错误是

Property 'delegate' with type 'WeekViewControllerDelegate?' cannot override a property with type 'MGCDayPlannerEKViewControllerDelegate!' (aka 'ImplicitlyUnwrappedOptional<MGCDayPlannerEKViewControllerDelegate>')

我做错了什么?

1 个答案:

答案 0 :(得分:3)

此错误表示在您的父类中有一个与其子级中具有相同名称的属性。在您的代码WeekViewControllerMGCDayPlannerEKViewController中有一个名为delegate的属性。您需要在班级delegateMGCDayPlannerEKViewController中重命名变量WeekViewController