Extensions Flashcards
TF: You can add computed properties to extensions?
True
TF: You can add stored properties or property observers?
False
TF: You can add new type and instance methods.
True
TF: You can define nested types
True
TF: Can add convenience initializers
True
TF: We can add protocol conformance to both custom types, native types and types from external frameworks in an extension
True
TF:You cannot provide a default implementation for any requirements specified in a protocol
False
TF: We can only provide default implementations for protocols via an extension.
True
What’s the purpose of using an Extension?
Extensions add new functionality to an existing class, structure, enumeration, or protocol type.
TF: Extensions can add new functionality to a type, but they cannot override existing functionality.
True
protocol SomeProtocol { func someMethod() -> Int }
extension SomeProtocol { func someMethod() -> Int { return 1 } }
struct SomeStruct: SomeProtocol { func someMethod() -> Int { return 2 } }
let b: SomeProtocol = SomeStruct()
b.someMethod()
What is the value returned from the someMethod() call?
1
Why can we not add designated initializers in a class extension?
A designated initializer initializes all properties of a class and we don't have access to private properties in an extension A designated initializer needs to call a specific superclass' initializer which is not possible from the extension.
protocol SomeProtocol { func someMethod() -> Int }
extension SomeProtocol { func someMethod() -> Int { return 1 } }
struct SomeStruct: SomeProtocol { func someMethod() -> Int { return 2 } }
let a: SomeStruct = SomeStruct()
a.someMethod()
What is the value returned from the someMethod() call
2
Which of following is not allowed in an extension? (Instance Methods, Stored Properties, Type Methods, Computed Properties)
Stored Properties