简体   繁体   English

从父 class Swift 的实例初始化子 Class

[英]Initialize a Child Class from an instance of Parent class Swift

I am kind of new to OOP (I think I understand the basics now), so I'm struggling with something:我对 OOP 有点陌生(我想我现在了解基础知识),所以我正在努力解决一些问题:
I am currently making a personal iOS app that involves using events from the iPhone Calendar.我目前正在制作一个涉及使用 iPhone 日历中的事件的个人 iOS 应用程序。 I wanted to add properties to the built in EKEvent class but I read that's not possible with extensions.我想向内置的EKEvent class 添加属性,但我读到扩展是不可能的。
So I decided to make my own Class, called Event , that will inherit from EKEvent and add the properties I want.所以我决定制作我自己的 Class,称为Event ,它将继承自EKEvent并添加我想要的属性。

But I'm struggling with the initialization for this class: I would like to initialize Event from an instance of EKEvent since I'm getting EKEvents when I fetch the events of my Calendar, but I can't find a way to do it.但是我在初始化这个 class 时遇到了困难:我想从EKEvent的实例中初始化Event ,因为当我获取日历的事件时我得到了 EKEvents,但我找不到办法做到这一点。 I looked but didn't find a similar questions for Swift, they are using super.init() which is not what I want to do.我看了但没有找到 Swift 的类似问题,他们使用的是 super.init(),这不是我想做的。

Here's my class, I found a workaround as you can see but I'd like to understand how to do it with inheritance if it's possible ^^这是我的 class,我找到了一个解决方法,如您所见,但我想了解如何使用 inheritance 如果可能的话^^

class Event {    // Deleted the : EKEvent here to test my workaround
    public var matiere: String = ""
    public var matiereId: String = ""
    public var prof: String = ""
    public var salle: String = ""
    public var type: String = ""
    
    public var ekEvent: EKEvent
    public var duration: DateInterval
    public var hoursDuration: Int
    public var minutesDuration: Int
    
    init(ekEvent: EKEvent) {
        /* Here is my workaround, if I need to access properties from EKEvent I'll use
        self.ekEvent.startDate (for example) */
        self.ekEvent = ekEvent

        self.duration = DateInterval(start: ekEvent.startDate, end: ekEvent.endDate)
        
        self.hoursDuration = Int(duration.duration) / 3600
        self.minutesDuration = (Int(self.duration.duration) - (3600 * self.hoursDuration)) / 60
    }
    
}

Thanks in advance and excuse my English I'm French =D提前谢谢并原谅我的英语我是法国人=D

glad to hear you're getting into OOP!很高兴听到您进入 OOP!

So the way you've seen people doing this using super.init() is the most common and accepted way to achieve what you want, but in this case, the init function that you would want to override hasn't been marked as open .因此,您看到人们using super.init()执行此操作的方式是实现您想要的最常见和公认的方式,但在这种情况下,您想要覆盖的 init function 尚未标记为open .

This essentially means you can't override it and init your own variables, then call super.init.这实质上意味着您不能覆盖它并初始化您自己的变量,然后调用 super.init。 So your approach is completely fair in this case.因此,在这种情况下,您的方法是完全公平的。

Streamline the process简化流程

so, as an idea, if you're aiming to try and make this a little more streamline, you could do something like this:因此,作为一个想法,如果您打算尝试使其更加精简,您可以执行以下操作:

extension EKEvent {
    
    func createEvent() -> Event {
        .init(ekEvent: self)
    }
    
}

This will allow you to now do this: ekEvent.createEvent() and then this will return an event object which you can use.这将允许您现在执行此操作: ekEvent.createEvent() ,然后这将返回您可以使用的事件 object。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM