简体   繁体   English

Apple Swift:在另一个 class 中初始化一个 class

[英]Apple Swift: Initialize a class in another class

I have some trouble to init() a class in another class.我在另一个 class 中init() class 时遇到了一些麻烦。 I've been looking if I can find a solution in here but I wasn't able to.我一直在寻找是否可以在这里找到解决方案,但我无法找到。

If I write super.init() there comes another error because the function isn't existing.如果我写 super.init() 会出现另一个错误,因为 function 不存在。 I don't know where I have to initialize it.我不知道我必须在哪里初始化它。 I'd prefer to init the Address class in the open init from the Contact class but if I do so I can't access the Address class.我更喜欢在Contact class 的打开初始化中初始化Address class,但如果这样做,我将无法访问Address class。

I think that it isn't a big mistake but I'm not able to find it.我认为这不是一个大错误,但我无法找到它。

open class Contact: Identifiable {
    public var id: Int64?
    var FirstName: String
    var Name: String
    var Phone: String
    var Mail: String
    var Birth: String
    var News: Bool

    open class Adress: Contact {
        var Street: String
        var Number: String
        var PostalCode: String
        var City: String
        var Country: String // Error:'super.init' isn't called on all paths before returning from initializer 
                            //If I add the Super.Init() there is an error because Super.Init() isn't existing and I don't know where to create it. 

        init(Street: String, Number: String, PostalCode: String, City: String, Country: String) {
            self.Street=Street
            self.Number=Number
            self.PostalCode=PostalCode
            self.City=City
            self.Country=Country

        }
    }
    public init(id: Int64, Firstname: String, Name: String, Phone: String, Mail: String, Birth: String, News: Bool) {
        self.id = id
        self.FirstName = Name
        self.Name = Name
        self.Phone = Phone
        self.Mail = Mail
        self.Birth = Birth
        self.News = false
    }   
}

Technically you could do it this way:从技术上讲,您可以这样做:

class Contact: Identifiable {
    public var id: Int64
    var name: String

    class Address: Contact {
        var street: String

        init(street: String, contact: Contact) {
            self.street = street
            super.init(id: contact.id, name: contact.name)
        }
    }
    public init(id: Int64, name: String) {
        self.id = id
        self.name = name
    }
}

let contact = Contact(id: 1, name: "name")
let address = Contact.Address(street: "street", contact: contact)

But maybe you want to model it in a way that a Contact has an Address :但也许你想 model 以ContactAddress的方式:

class Contact: Identifiable {
    public var id: Int64
    var name: String
    var address: Address

    public init(id: Int64, name: String, address: Address) {
        self.id = id
        self.name = name
        self.address = address
    }
}

class Address: Identifiable {
    var id: Int64
    var street: String

    public init(id: Int64, street: String) {
        self.id = id
        self.street = street
    }
}

let address = Address(id: 1, street: "street")
let contact = Contact(id: 2, name: "name", address: address)

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

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