Hint: Always add { get } or { get set } to protocol properties [OK]
Common Mistakes:
Omitting { get } or { get set } in protocol properties
Adding parentheses to property declarations
Thinking protocol names must be uppercase
5. You want to declare a protocol Identifiable that requires a read-only id property of type String and a method identify() that returns a String. Which is the correct declaration?
hard
A. protocol Identifiable {
let id: String
func identify() -> String
}
B. protocol Identifiable {
var id: String
func identify()
}
C. protocol Identifiable {
var id: String { get set }
func identify() -> String
}
D. protocol Identifiable {
var id: String { get }
func identify() -> String
}
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 method identify() must specify return type String as required.
Step 3: Evaluate options
protocol Identifiable {
var id: String { get }
func identify() -> String
} correctly declares var id: String { get } and func identify() -> String. protocol Identifiable {
var id: String
func identify()
} misses { get } and return type. protocol Identifiable {
let id: String
func identify() -> String
} uses let which 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 D
Quick Check:
Read-only property uses { get } and method return type matches [OK]
Hint: Use { get } for read-only properties in protocols [OK]
Common Mistakes:
Using 'let' instead of 'var' in protocol
Omitting { get } for read-only properties
Missing return type in method declaration
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.