An associated type is a placeholder name in a protocol that represents a type to be specified by the conforming type. It allows protocols to work with types that are not yet known.
Click to reveal answer
beginner
How do you declare an associated type inside a Swift protocol?
Use the keyword associatedtype followed by the name of the placeholder type inside the protocol definition.
Click to reveal answer
intermediate
Why are associated types useful in protocols?
They let protocols be flexible and reusable by allowing the conforming types to specify the exact types they work with, enabling generic-like behavior.
Click to reveal answer
beginner
Can you give a simple example of a protocol with an associated type?
Yes. For example:
protocol Container {
associatedtype Item
mutating func append(_ item: Item)
var count: Int { get }
subscript(i: Int) -> Item { get }
}
Click to reveal answer
intermediate
How does a type conform to a protocol with an associated type?
The conforming type must specify the actual type for the associated type, either explicitly or by inference through its implementation.
Click to reveal answer
What keyword is used to declare an associated type in a Swift protocol?
Aassociatedtype
Btypealias
Cgeneric
Dtypedef
✗ Incorrect
The correct keyword to declare an associated type in a Swift protocol is associatedtype.
Which of the following best describes an associated type?
AA concrete type defined inside a protocol
BA placeholder type in a protocol to be specified by conforming types
CA function inside a protocol
DA variable inside a protocol
✗ Incorrect
An associated type is a placeholder type in a protocol that conforming types specify.
If a protocol has an associated type, how does a conforming type specify it?
ABy declaring the associated type inside the conforming type
BBy using a global typealias
CBy inheriting from another protocol
DBy implementing the protocol’s requirements using a specific type
✗ Incorrect
The conforming type specifies the associated type by implementing the protocol’s requirements with a concrete type.
Can a protocol with an associated type be used as a type directly?
ANo, because the associated type is not specified
BYes, always
COnly if the associated type is Int
DOnly if the protocol has no methods
✗ Incorrect
Protocols with associated types cannot be used as types directly because the associated type is a placeholder that must be specified.
What is a common use case for associated types in Swift protocols?
ATo override methods in classes
BTo define stored properties in protocols
CTo create flexible, generic-like protocols that work with different types
DTo create global variables
✗ Incorrect
Associated types allow protocols to be flexible and generic-like, working with different types specified by conforming types.
Explain what an associated type is in a Swift protocol and why it is useful.
Think about how protocols can work with unknown types.
You got /4 concepts.
Describe how a type conforms to a protocol that has an associated type.
Focus on how the placeholder type gets a real type.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of associatedtype in a Swift protocol?
easy
A. To create a stored property inside the protocol
B. To define a placeholder type that conforming types will specify
C. To implement a method inside the protocol
D. To inherit from another protocol
Solution
Step 1: Understand the role of associatedtype
The associatedtype keyword declares a placeholder type inside a protocol.
Step 2: Identify how conforming types use it
Types that adopt the protocol specify the actual type for this placeholder, making the protocol flexible.
Final Answer:
To define a placeholder type that conforming types will specify -> Option B
Quick Check:
Associated types = Placeholder type [OK]
Hint: Remember: associatedtype is a placeholder type in protocols [OK]
Common Mistakes:
Thinking associatedtype creates stored properties
Confusing associatedtype with method implementation
Assuming associatedtype inherits protocols
2. Which of the following is the correct syntax to declare an associated type named Item inside a protocol?
easy
A. protocol Example { typealias Item }
B. protocol Example { func Item() }
C. protocol Example { var Item: Int }
D. protocol Example { associatedtype Item }
Solution
Step 1: Recall the syntax for associated types
The correct keyword to declare an associated type is associatedtype.
Step 2: Match the syntax with options
protocol Example { associatedtype Item } uses associatedtype Item, which is the correct syntax inside a protocol.
Final Answer:
protocol Example { associatedtype Item } -> Option D
Quick Check:
Syntax for associatedtype = protocol Example { associatedtype Item } [OK]
Hint: Use 'associatedtype' keyword, not 'typealias' for protocols [OK]
Common Mistakes:
Using typealias instead of associatedtype
Declaring associated type as a variable
Trying to declare associated type as a function
3. Consider the following Swift code:
protocol Container {
associatedtype Item
func append(_ item: Item)
var count: Int { get }
subscript(i: Int) -> Item { get }
}
struct IntStack: Container {
var items = [Int]()
mutating func append(_ item: Int) {
items.append(item)
}
var count: Int { items.count }
subscript(i: Int) -> Int {
items[i]
}
}
What is the type of Item for IntStack?
medium
A. String
B. Double
C. Int
D. Any
Solution
Step 1: Identify the associated type in the protocol
The protocol Container has an associated type Item.
Step 2: Check the conforming type's implementation
IntStack uses an array of Int and its append method accepts Int. So, Item is Int.
Final Answer:
Int -> Option C
Quick Check:
Associated type Item = Int in IntStack [OK]
Hint: Look at the type used in methods and properties for Item [OK]
A. append method parameter type does not match associated type
B. Missing mutating keyword on append method
C. Associated type Item is not specified
D. items array type mismatch
Solution
Step 1: Check the protocol's associated type and method
The protocol expects append(_ item: Item) where Item is the associated type.
Step 2: Compare with struct's append method
StringStack has append(_ item: Int), but Item should be String because items is an array of String.
Step 3: Identify mismatch
The parameter type Int does not match the expected String associated type.
Final Answer:
append method parameter type does not match associated type -> Option A
Quick Check:
Parameter type mismatch = append method parameter type does not match associated type [OK]
Hint: Check method parameter matches associated type exactly [OK]
Common Mistakes:
Forgetting to mark append as mutating
Ignoring parameter type mismatch
Assuming associated type is Int automatically
5. You want to create a protocol PairContainer with two associated types First and Second. You want a function swap() that swaps the two values inside a conforming struct. Which of the following is the correct way to declare the protocol and implement swap() in a struct?
hard
A. protocol PairContainer {
associatedtype First
associatedtype Second
var first: First { get set }
var second: Second { get set }
mutating func swap()
}
struct Pair: PairContainer {
var first: Int
var second: Int
mutating func swap() {
(first, second) = (second, first)
}
}
B. protocol PairContainer {
associatedtype First
associatedtype Second
var first: First { get }
var second: Second { get }
func swap()
}
struct Pair: PairContainer {
var first: Int
var second: Int
func swap() {
let temp = first
first = second
second = temp
}
}
C. protocol PairContainer {
associatedtype First
associatedtype Second
var first: First { get set }
var second: Second { get set }
func swap()
}
struct Pair: PairContainer {
var first: Int
var second: Int
mutating func swap() {
(first, second) = (second, first)
}
}
D. protocol PairContainer {
associatedtype First
associatedtype Second
var first: First { get set }
var second: Second { get set }
mutating func swap()
}
struct Pair: PairContainer {
var first: Int
var second: Int
func swap() {
(first, second) = (second, first)
}
}
Solution
Step 1: Define protocol with mutable properties and mutating method
The protocol requires first and second as get/set properties and a mutating func swap() to modify them.
Step 2: Implement struct with matching mutating swap method
The struct Pair has mutable first and second properties and a mutating func swap() that swaps values correctly.
Step 3: Check other options for errors
protocol PairContainer {
associatedtype First
associatedtype Second
var first: First { get }
var second: Second { get }
func swap()
}
struct Pair: PairContainer {
var first: Int
var second: Int
func swap() {
let temp = first
first = second
second = temp
}
}'s properties are get-only and swap is non-mutating, so it cannot modify properties. protocol PairContainer {
associatedtype First
associatedtype Second
var first: First { get set }
var second: Second { get set }
func swap()
}
struct Pair: PairContainer {
var first: Int
var second: Int
mutating func swap() {
(first, second) = (second, first)
}
} has non-mutating protocol method but mutating struct method, mismatch. protocol PairContainer {
associatedtype First
associatedtype Second
var first: First { get set }
var second: Second { get set }
mutating func swap()
}
struct Pair: PairContainer {
var first: Int
var second: Int
func swap() {
(first, second) = (second, first)
}
} has mutating protocol method but non-mutating struct method, mismatch.
Final Answer:
Protocol with { get set } properties and mutating func swap(), struct with mutating func swap() -> Option A
Quick Check:
Mutating methods and get/set properties = protocol PairContainer {
associatedtype First
associatedtype Second
var first: First { get set }
var second: Second { get set }
mutating func swap()
}
struct Pair: PairContainer {
var first: Int
var second: Int
mutating func swap() {
(first, second) = (second, first)
}
} [OK]
Hint: Mutating methods must be marked mutating in protocol and struct [OK]
Common Mistakes:
Not marking swap as mutating in protocol or struct
Using get-only properties when set is needed
Mismatch between protocol and struct method mutability
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.