Question: I really need help with this Swift code. Can anyone help with creating this class? Write a Swift class based on the following requirements and
I really need help with this Swift code. Can anyone help with creating this class?
Write a Swift class based on the following requirements and test it using the provided code.
The name of the class: Document
The class has the following properties: title body length
title is of type String. It is not an optional. When it is declared it is not set to an initial value. Since it is not an optional and does not have a value set when it is declared, the class init must set the title when a Document instance is created.
body is of type String. It is not an optional and is set to an empty string ("") when it is declared.
length is a computed property of type Int. The length property has only a getter that returns the number of characters in the body string. The number of characters in the body can be determined using: body.characters.count
The title and body are to modifiable. They are not to be implemented as constants.
The Document class is to have two initializers. One initializer is to receive only the title. The other initializer is to receive a title and a body.
Test the Document class using the following code: let document1 = Document(title: "Hello World") document1.title = "Hello World!" document1.body = "I awoke from a digital slumber." let document2 = Document(title: "The Day", body: "It was a day to remember.") print(document1.title) print(document1.body) print(document1.length) print(document2.title) print(document2.length)
The output should be: Hello World! I awoke from a digital slumber. 31 The Day 25
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
