📖 Scenario: You are building a simple app that manages different types of vehicles. Each vehicle should be able to describe itself.
🎯 Goal: Create a protocol called Vehicle with a property and a method. Then create a struct that conforms to this protocol and implements the required property and method.
📋 What You'll Learn
Create a protocol named Vehicle with a var name: String { get } property and a func description() -> String method.
Create a struct named Car that conforms to the Vehicle protocol.
Implement the name property and the description() method inside Car.
Print the result of calling description() on an instance of Car.
💡 Why This Matters
🌍 Real World
Protocols help define common behavior for different types, like vehicles, so your app can work with them in a uniform way.
💼 Career
Understanding protocol conformance is essential for writing clean, reusable, and flexible Swift code in professional iOS development.
Progress0 / 4 steps
1
Create the Vehicle protocol
Create a protocol called Vehicle with a read-only property name of type String and a method description() that returns a String.
Swift
Hint
Use the protocol keyword to define a protocol. The property name should be read-only, so use { get }.
2
Create the Car struct conforming to Vehicle
Create a struct named Car that conforms to the Vehicle protocol. Add a stored property name of type String.
Swift
Hint
Use struct Car: Vehicle to conform. Implement the name property as a stored property.
3
Implement the description() method in Car
Inside the Car struct, implement the description() method to return the string "This is a car named \(name).".
Swift
Hint
Return a string using string interpolation to include the name property.
4
Create an instance of Car and print its description
Create a constant myCar of type Car with the name "SwiftCar". Then print the result of calling myCar.description().
Swift
Hint
Create the instance with Car(name: "SwiftCar") and print the description.
Practice
(1/5)
1. What does it mean for a Swift type to conform to a protocol?
easy
A. It can only be a class, not a struct or enum.
B. It inherits all properties and methods from the protocol automatically.
C. It implements all required methods and properties defined by the protocol.
D. It must override all methods from the protocol.
Solution
Step 1: Understand protocol requirements
A protocol defines methods and properties that a type must implement to conform.
Step 2: Check what conformance means
Conforming means the type provides implementations for all required members.
Final Answer:
It implements all required methods and properties defined by the protocol. -> Option C
Quick Check:
Protocol conformance = Implement required members [OK]
Hint: Conformance means implementing all required protocol parts [OK]
Common Mistakes:
Thinking conformance means inheritance
Believing only classes can conform
Assuming methods must be overridden
2. Which of the following is the correct syntax to declare that a struct conforms to a protocol named Drawable?
easy
A. struct Shape inherits Drawable { }
B. struct Shape : Drawable { }
C. struct Shape implements Drawable { }
D. struct Shape extends Drawable { }
Solution
Step 1: Recall Swift syntax for protocol conformance
Swift uses a colon (:) after the type name to declare protocol conformance.
Step 2: Match syntax options
Only struct Shape : Drawable { } uses the correct colon syntax for protocol conformance.
Final Answer:
struct Shape : Drawable { } -> Option B
Quick Check:
Protocol conformance syntax uses ':' [OK]
Hint: Use ':' after type name to conform to protocol [OK]