Open menu with table of contents iOS Assignment
Logo of Stuttgart Media University for light theme Logo of Stuttgart Media University for dark theme
Mobile Application Development 2

iOS Assignment

Properties

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.

Stuttgart Media University

1 Tasks

  1. Create Person class with fullName Property

2 1. Create Person class with fullName Property

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)")

2.1 Tip

To find the space between the first- and last name you can use components(separatedBy: " ") method of the String class.

3 Tasks

  1. Create a FriendlyPerson Protocol

4 2. Create a FriendlyPerson Protocol

Extend your previously created Person class and add a custom initializer method that sets the first name and last name Properties right on object creation.

Currently the person is unfriendly because it is not able to say hello. Create a FriendlyPerson protocol that specifies a few methods that make a friendly person. Let the person class implement the protocol and call some of the friendly methods.

As the last step, create a Person instance and test the implementation

4.1 Tip

You can define the FriendlyPerson protocol in the Playground just before the Person class.