Please conduct the following tasks alone. For implementation details you can refer to the lecture slides or the Apple developer website. Please do not hesitate to ask me or the tutor if you have any questions. Under “Tip” you can find some hints for the task. The “Checklist” specifies what should happen when your implementation is correct.
Open a new Playground and create a class called Person with the following three Properties: + firstName: String + lastName: String + fullName: String
The fullName property has a getter and a setter. In the getter, return a String that combines the firstName and the lastName Property like this:
„firstName lastName“.
In the setter, the input value is a String that should contain a first name, a space character and a last name, such as: „firstname lastname“. You should now parse the String and set the firstName Property and the lastName Property correspondingly. When using the class the results should be like in the example code below:
let person = Person(firstname: "Otto", lastname: "Hahn")
print("Firstname: \(person.firstname) Lastname: \(person.lastname)")
print("Fullname: \(person.fullname)")
person.fullname = "Hans Meiser"
print("Firstname: \(person.firstname) Lastname: \(person.lastname)")
print("Fullname: \(person.fullname)")
To find the space between the first- and last name you can use components(separatedBy: “ “) method of the String class.
Checklist | |
---|---|
When you set the fullName property the firstName and lastName properties are set correspondingly |