Extensions Flashcards

Learn about extensions as is explained in "The Swift Programming language" official book from Apple

1
Q

You can use 1._____ to add functionality to an existing type, such as new 2.____ and 3.______ _______

A
  1. extension 2. methods 3. computed properties
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

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. ______

A
  1. extension 2. elsewhere 3. type 4. library 5. Framework
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What do “extensions” do?

A

They add functionality to an existing class, structure, enumeration, or protocol type.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is retroactive modeling?

A

The ability to extend types for which you do not have access to the original source code. Extensions include retroactive modeling.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Extensions are similar to _____ in Objective-C.

A

categories

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Unlike Objective-C categories, Swift extensions do not have _____.

A

names

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are six things that extensions in Swift can do?

A
  1. Add computed instance properties and computed type properties
  2. Define instance methods and type methods
  3. Provide new initializers
  4. Define subscripts
  5. Define and use new nested types
  6. Make an existing type conform to a protocol
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Extensions can add new functionality to a type, but they can not _____ ______ ______.

A

Override existing functionality

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

We can declare extensions with the ______ ______.

A

extension keyword

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Syntax to declare extension with the extension keyword

A
extension SomeType {
   //   new functionality to add to SomeType goes here
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

An extension can extend an existing type to make it adopt one or more _______.

A

protocols

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you add protocol conformance when declaring an extension?

A

You write the protocol names the same way as you would write them for a class or structure.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Syntax for an extension that extends an existing type to make it adopt multiple protocols

A
extension SomeType: SomeProtocol, AnotherProtocol {
   // implementation of protocol requirements goes here
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

If you define an extension to add new functionality to an existing type, where will this new functionality be available?

A

It will be available on all existing instances of that type, even if they were created before the extension was defined.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

An extension can be used to extend an existing generic type. You can also extend a generic type to _____ ______ ______.

A

Conditionally add functionality

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Extensions can add computed 1. ______ properties and computed 2.______ properties to existing types.

A
  1. instance 2. type
17
Q

Extensions can add new computed properties, but they can not add 1. _____ ______, or add 2. _____ _____ to existing properties.

A
  1. stored properties

2. property observers

18
Q

Extensions can add new initializers to existing types. What does this enable you to do? (List 2 things)

A
  1. To extend other types to accept your own custom types as initializer parameters
  2. To provide additional initialization options that were not included as part of the type’s original implementation.
19
Q

Extensions can add new convenience initializers to a class, but they can’t add new 1. _____ ______ or 2. _____ to a class.

A
  1. Designated initializers

2. Deinitializers

20
Q

Designated initializers and deinitializers must always be provided by the ____ _____ _____

A

original class implementation