0
0
Swiftprogramming~5 mins

Extension syntax in Swift - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is an extension in Swift?
An extension in Swift adds new functionality to an existing class, struct, enum, or protocol without modifying the original source code.
Click to reveal answer
beginner
How do you declare an extension for a struct named Person?
Use the syntax: <br>
extension Person {<br>    // new methods or properties<br>}
Click to reveal answer
intermediate
Can extensions add stored properties to types in Swift?
No, extensions cannot add stored properties. They can only add computed properties, methods, initializers, and conform to protocols.
Click to reveal answer
beginner
What is the benefit of using extensions in Swift?
Extensions help organize code better, add functionality to types you don’t own, and conform to protocols without subclassing.
Click to reveal answer
beginner
Show an example of adding a method greet() to a struct Person using extension.
extension Person {<br>    func greet() {<br>        print("Hello!")<br>    }<br>}
Click to reveal answer
Which of the following can you NOT add using a Swift extension?
AComputed properties
BMethods
CStored properties
DInitializers
How do you start an extension declaration for a class named Car?
Aextension Car { }
Bclass Car extension { }
Cextend Car { }
DCar extension { }
Can extensions in Swift add protocol conformance to existing types?
ANo
BYes
COnly for classes
DOnly for structs
Which keyword is used to declare an extension in Swift?
Amodify
Bextend
Cadd
Dextension
What can extensions NOT do?
AOverride existing methods
BAdd computed properties
CAdd new methods
DAdd initializers
Explain what Swift extensions are and why they are useful.
Think about how you can add features to existing types without changing their original code.
You got /5 concepts.
    Describe the syntax of declaring an extension and what you can add inside it.
    Remember the basic structure and what features extensions support.
    You got /7 concepts.