Question

Multiple Choice: All of the following are true about Swift extensions except: Protocols can be extended...

Multiple Choice:

All of the following are true about Swift extensions except:

  1. Protocols can be extended with method implementations
  2. You can extend value types
  3. You must have access to the source code of the type you extend
  4. You can extend reference types
0 0
Add a comment Improve this question Transcribed image text
Answer #1

You can extend an existing type to adopt and conform to a new protocol, even if you don’t have access to the source code for the existing type. Extensions can add new properties, methods, and subscripts to an existing type, and are therefore able to add any requirements that a protocol may demand.

For example, this protocol, called TextRepresentable, can be implemented by any type that has a way to be represented as text. This might be a description of itself, or a text version of its current state:

  1. protocol TextRepresentable {
  2. var textualDescription: String { get }
  3. }

The Dice class from above can be extended to adopt and conform to TextRepresentable:

  1. extension Dice: TextRepresentable {
  2. var textualDescription: String {
  3. return "A \(sides)-sided dice"
  4. }
  5. }

This extension adopts the new protocol in exactly the same way as if Dice had provided it in its original implementation. The protocol name is provided after the type name, separated by a colon, and an implementation of all requirements of the protocol is provided within the extension’s curly braces.

So, Answer is :

You must have access to the source code of the type you extend

Add a comment
Know the answer?
Add Answer to:
Multiple Choice: All of the following are true about Swift extensions except: Protocols can be extended...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT