A protocol in Swift is like a promise that a type will have certain properties or methods. It helps organize code and makes sure things work together smoothly.
Protocol declaration syntax in Swift
protocol ProtocolName { // property and method requirements }
The keyword protocol starts the declaration.
Inside the braces, you list what properties or methods any type adopting this protocol must have.
drive() method.protocol Drivable { func drive() }
name of type String.protocol Named { var name: String { get } }
id that can be read and changed.protocol Identifiable { var id: Int { get set } }
This program defines a protocol Greetable with a method greet(). The struct Person adopts this protocol and implements the method. When we create a Person and call greet(), it prints a greeting.
protocol Greetable { func greet() } struct Person: Greetable { func greet() { print("Hello! Nice to meet you.") } } let someone = Person() someone.greet()
Protocols only describe what is required, not how it works.
Types like classes, structs, and enums can adopt protocols.
Protocols help make your code easier to understand and maintain.
Protocols define a set of rules (methods or properties) that types must follow.
Use protocol keyword to declare a protocol.
Protocols help write flexible and organized code.
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
