0
votes

I am learning swift on my own but I cannot manage to make this code run, I am trying to create a class "Inhabitant" inheriting from Person class with a new "Country" property, have 2 initializers: - one that takes firstname and lastname and sets the country to an empty String - one that takes firstname, lastname and country and create a computed property description of String type that return a the firstname, lastname and country separated by a space

class Person {
    var firstname: String
    var lastname: String
    var fullname: String {
        return firstname + " " + lastname
    }

    init(firstname: String, lastname: String) {
        self.firstname = firstname
        self.lastname = lastname
    }
}

    class Inhabitant: Person {
    var country: String? = nil
    var description: String {
        return firstname + " " + lastname + " " + country ?? ""
    }
    override init(firstname: String, lastname: String) {
        super.init(firstname: <#T##String#>, lastname: <#T##String#>)
        self.country = ""
    }
    init(firstname: String, lastname: String, Country: String) {
        super.init(firstname: <#T##String#>, lastname: <#T##String#>)
    }
}

func tst() {
    let perso = Inhabitant(firstname: "toto", lastname: "bobo")
    print(perso)
}

The code won't run:

Editor placeholder in source file
1
Change <#T##String#> to some String. The easy way is select it and type if it is still a "live" placeholder; otherwise you'll just have to do it by hand. Learn about Xcode's "autocomplete" feature. What you want are firstname and lastname here. In other words, you just want to pass the incoming firstname and lastname values up the inheritance chain to the super.init call. - matt
In Xcode, see the word String after super.init(firstname:? That's an "editor placeholder". You need to replace that with something. - Sweeper
@Sweeper Thanks for resetting my brain - early morning here! - matt
But why should I replace it ? I want to call this string inside the tst function, not in the initialiser - catrev
You need to pass the variables into where those placeholders are. i.e. super.init(firstname: firstname, lastname: lastname) - clawesome

1 Answers

0
votes

I would do something like this:

class Person {
    var firstname: String
    var lastname: String
    var fullname: String {
        return firstname + " " + lastname
    }

    init(firstname: String, lastname: String) {
        self.firstname = firstname
        self.lastname = lastname
    }
}

class Inhabitant: Person {
    var country: String? = nil
    var description: String {
        //1
        return "\(firstname) \(lastname) \(country ?? "")"
    }
    override init(firstname: String, lastname: String) {
        //2
        super.init(firstname: firstname, lastname: lastname)
        self.country = ""
    }
    init(firstname: String, lastname: String, country: String) {
        super.init(firstname: firstname, lastname: lastname)
        self.country = country
    }
}

func tst() {
    let perso = Inhabitant(firstname: "toto", lastname: "bobo")
    let perso2 = Inhabitant(firstname: "toto", lastname: "bobo", country: "Belgium")
    print(perso.fullname)
    print(perso2.description)
}

tst()

Explanations:

  1. String interpolation in swift are done with this syntax, cf swift programming language
  2. When you override the initialiser, what you want is to pass the arguments you receive to your super initialiser.