Extensions Flashcards
Learn about extensions as is explained in "The Swift Programming language" official book from Apple
You can use 1._____ to add functionality to an existing type, such as new 2.____ and 3.______ _______
- extension 2. methods 3. computed properties
You can use 1. ______ to add protocol conformance to a type that is declared 2. ______ or even a 3. _____ that you imported from a 4._____ or 5. ______
- extension 2. elsewhere 3. type 4. library 5. Framework
What do “extensions” do?
They add functionality to an existing class, structure, enumeration, or protocol type.
What is retroactive modeling?
The ability to extend types for which you do not have access to the original source code. Extensions include retroactive modeling.
Extensions are similar to _____ in Objective-C.
categories
Unlike Objective-C categories, Swift extensions do not have _____.
names
What are six things that extensions in Swift can do?
- Add computed instance properties and computed type properties
- Define instance methods and type methods
- Provide new initializers
- Define subscripts
- Define and use new nested types
- Make an existing type conform to a protocol
Extensions can add new functionality to a type, but they can not _____ ______ ______.
Override existing functionality
We can declare extensions with the ______ ______.
extension keyword
Syntax to declare extension with the extension keyword
extension SomeType { // new functionality to add to SomeType goes here }
An extension can extend an existing type to make it adopt one or more _______.
protocols
How do you add protocol conformance when declaring an extension?
You write the protocol names the same way as you would write them for a class or structure.
Syntax for an extension that extends an existing type to make it adopt multiple protocols
extension SomeType: SomeProtocol, AnotherProtocol { // implementation of protocol requirements goes here }
If you define an extension to add new functionality to an existing type, where will this new functionality be available?
It will be available on all existing instances of that type, even if they were created before the extension was defined.
An extension can be used to extend an existing generic type. You can also extend a generic type to _____ ______ ______.
Conditionally add functionality