0
0
iOS Swiftmobile~5 mins

Protocols in iOS Swift - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a protocol in Swift?
A protocol defines a blueprint of methods, properties, and other requirements that suit a particular task or piece of functionality. Classes, structs, or enums can adopt protocols to provide actual implementations.
Click to reveal answer
beginner
How do you declare a protocol in Swift?
Use the protocol keyword followed by the protocol name and curly braces. Inside, list the required methods or properties without implementations.
Click to reveal answer
intermediate
Can a Swift protocol require properties? If yes, what kind?
Yes, protocols can require properties. They can specify whether the property must be gettable or gettable and settable, but they do not provide storage or implementation.
Click to reveal answer
beginner
What does it mean for a type to conform to a protocol?
It means the type agrees to implement all the requirements defined in the protocol, such as methods and properties, ensuring it meets the protocol's contract.
Click to reveal answer
intermediate
Can protocols be used as types in Swift? Give an example.
Yes, protocols can be used as types to define variables, parameters, or return types. For example, a variable of type MyProtocol can hold any instance of a type that conforms to MyProtocol.
Click to reveal answer
Which keyword is used to define a protocol in Swift?
Aclass
Bprotocol
Cinterface
Dstruct
What must a type do to conform to a protocol?
AImplement all required methods and properties
BInherit from the protocol
COverride the protocol
DDeclare the protocol as a variable
Can a protocol specify property requirements without providing storage?
ANo, it must provide storage
BOnly for classes
CYes, it only specifies get/set requirements
DOnly for structs
Which of these can adopt a protocol in Swift?
AClass, struct, and enum
BOnly classes
COnly structs
DOnly enums
Can you use a protocol as a type for a variable?
AOnly inside functions
BNo, protocols are not types
COnly if the protocol is a class
DYes, to hold any conforming instance
Explain what a protocol is in Swift and why it is useful.
Think about how protocols help different types agree on a set of rules.
You got /4 concepts.
    Describe how a type conforms to a protocol and what happens if it does not implement all requirements.
    Consider what the compiler expects when you say a type adopts a protocol.
    You got /3 concepts.