Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a protocol named Logger with a log function.
iOS Swift
protocol Logger {
func [1](message: String)
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a function name that does not describe logging, like 'print' or 'send'.
✗ Incorrect
The protocol requires a function named 'log' to handle logging messages.
2fill in blank
mediumComplete the code to create a mock class MockLogger that conforms to Logger protocol.
iOS Swift
class MockLogger: Logger { var messages: [String] = [] func [1](message: String) { messages.append(message) } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different function name than the protocol requires.
✗ Incorrect
The mock class must implement the 'log' function to conform to the Logger protocol.
3fill in blank
hardFix the error in the test function to verify the mock logger recorded the message.
iOS Swift
func testLogging() {
let mockLogger = MockLogger()
mockLogger.log(message: "Test message")
assert(mockLogger.[1].count == 1)
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a property name that does not exist in MockLogger.
✗ Incorrect
The mock logger stores logged messages in the 'messages' array.
4fill in blank
hardFill both blanks to create a protocol and a mock class that counts calls.
iOS Swift
protocol [1] { func fetchData() } class MockFetcher: [2] { var callCount = 0 func fetchData() { callCount += 1 } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using mismatched protocol and class names.
✗ Incorrect
The protocol is named DataFetcher and the mock class conforms to it to count calls.
5fill in blank
hardFill all three blanks to define a protocol, a mock class, and a test verifying call count.
iOS Swift
protocol [1] { func save(data: String) } class MockSaver: [2] { var saveCalls = 0 func save(data: String) { saveCalls += 1 } } func testSave() { let mock = MockSaver() mock.save(data: "Hello") assert(mock.[3] == 1) }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect protocol or property names in the test.
✗ Incorrect
The protocol is Saver, the mock class conforms to Saver, and the test checks saveCalls count.