What if you could write a single set of rules that many different things promise to follow perfectly?
Why Protocol declaration syntax in Swift? - Purpose & Use Cases
Imagine you want to create several types of objects that share some common behavior, like different vehicles that can all start and stop. Without a clear plan, you write similar code for each vehicle separately.
Writing the same methods over and over is slow and easy to forget or make mistakes. If you want to change the behavior, you must update every single type manually, which is tiring and error-prone.
Protocols let you define a blueprint of methods and properties that many types can follow. This way, you write the rules once, and all types that adopt the protocol promise to follow them, making your code cleaner and easier to manage.
class Car { func start() { /* code */ } func stop() { /* code */ } } class Bike { func start() { /* code */ } func stop() { /* code */ } }
protocol Vehicle {
func start()
func stop()
}
class Car: Vehicle {
func start() { /* code */ }
func stop() { /* code */ }
}
class Bike: Vehicle {
func start() { /* code */ }
func stop() { /* code */ }
}Protocols enable you to write flexible and reusable code by defining shared behavior that many types can adopt.
Think of a remote control that works with any TV brand because all TVs follow the same control protocol. Similarly, protocols in Swift let different types work together smoothly.
Protocols define a set of rules for methods and properties.
They help avoid repeating code and reduce errors.
Many types can adopt the same protocol to share behavior.
Practice
Solution
Step 1: Recall Swift syntax for protocols
In Swift, protocols are declared using the keywordprotocol.Step 2: Compare options with known syntax
Options likeinterfaceare from other languages, andclassorstructdeclare types, not protocols.Final Answer:
protocol -> Option CQuick Check:
Keyword for protocol declaration = protocol [OK]
- Confusing 'protocol' with 'interface' from other languages
- Using 'class' or 'struct' instead of 'protocol'
- Misspelling the keyword
Vehicle in Swift?Solution
Step 1: Understand protocol declaration syntax
Protocols are declared with the keywordprotocol, followed by the name and curly braces without parentheses or brackets.Step 2: Check each option
protocol Vehicle {} uses correct syntax:protocol Vehicle {}. Options B, C, and D incorrectly add parentheses or brackets.Final Answer:
protocol Vehicle {} -> Option AQuick Check:
Protocol declaration uses curly braces only [OK]
- Adding parentheses after protocol name
- Using square or angle brackets incorrectly
- Omitting curly braces
protocol Greetable {
func greet() -> String
}
struct Person: Greetable {
func greet() -> String {
return "Hello!"
}
}
let p = Person()
print(p.greet())Solution
Step 1: Understand protocol and struct implementation
The protocolGreetablerequires a methodgreet()returning a String. The structPersonimplements this method returning "Hello!".Step 2: Analyze the print statement
Creating an instancepofPersonand callingp.greet()returns "Hello!", which is printed.Final Answer:
Hello! -> Option AQuick Check:
Implemented protocol method returns "Hello!" [OK]
- Assuming protocol methods run automatically without implementation
- Expecting error due to protocol conformance
- Confusing method name with output
protocol Drawable {
func draw()
var color: String
}Solution
Step 1: Check protocol property declaration rules
In Swift, protocol properties must declare if they are readable or writable using{ get }or{ get set }.Step 2: Analyze the given property
The propertycolor: Stringlacks{ get }or{ get set }, causing a syntax error.Final Answer:
Protocol properties must specify { get } or { get set } -> Option BQuick Check:
Protocol property syntax requires access specifiers [OK]
- Omitting { get } or { get set } in protocol properties
- Adding parentheses to property declarations
- Thinking protocol names must be uppercase
Identifiable that requires a read-only id property of type String and a method identify() that returns a String. Which is the correct declaration?Solution
Step 1: Define read-only property in protocol
A read-only property in a protocol must be declared with{ get }to indicate it can be read but not written.Step 2: Check method signature
The methodidentify()must specify return typeStringas required.Step 3: Evaluate options
protocol Identifiable { var id: String { get } func identify() -> String } correctly declaresvar id: String { get }andfunc identify() -> String. protocol Identifiable { var id: String func identify() } misses{ get }and return type. protocol Identifiable { let id: String func identify() -> String } usesletwhich is invalid in protocols. protocol Identifiable { var id: String { get set } func identify() -> String } allows writing with{ get set }, not read-only.Final Answer:
protocol Identifiable { var id: String { get } func identify() -> String } -> Option DQuick Check:
Read-only property uses { get } and method return type matches [OK]
- Using 'let' instead of 'var' in protocol
- Omitting { get } for read-only properties
- Missing return type in method declaration
Ready to prove it?
Pass the test and this pattern moves unknown
