0

I have some trouble to init() a class in another 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. 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.

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
    }   
}
Sulthan
  • 128,090
  • 22
  • 218
  • 270
Simon B
  • 3
  • 1
  • You use the `super.init()` inside the initialiser of the `Adress` class after initialising all the new properties inside the class. For that you have to add all properties you need for initialising `Contact` to the init attributes. On an unrelated topic: you normally do NOT capitalise properties such as variables or constants, that's considered bad practice. – timfraedrich May 10 '20 at 11:20
  • this might help you, https://stackoverflow.com/questions/26806932/swift-nested-class-properties – Asif Newaz May 10 '20 at 11:23
  • Using subclassing in this case is a very bad choice. Your problem arises from that choice. There is no reason why `Address` should be a subtype of `Contact`. If there is a connection between the two, it should be made using composition, not subclassing. – Sulthan May 10 '20 at 13:05
  • A Contact HAS-A Address, use this rule of thumb for composition, make an Address property inside the Contact class, and move the address class outside. – Jeet Dholakia May 10 '20 at 16:09

1 Answers1

0

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:

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)
Falkone
  • 161
  • 2
  • 4
  • Thanks that is exactly what I was looking for. Even though a composition is said to be the better solution. – Simon B May 10 '20 at 14:59
  • One more thing... In the let address = Adress(...) line there occurs a new error: 'Contact' cannot be constructed because it has no accessible initializers Thanks again for your help! – Simon B May 10 '20 at 15:25
  • Use the correct access level for the initializer that fits your environment, so putting `public` in front of the `init` in `Address` will work for you I guess. – Falkone May 10 '20 at 15:41