Join Scott Gardner for an in-depth discussion in this video External protocols, part 2, part of Swift 3 Essential Training: Beyond the Basics.
- [Instructor] In addition to extending a protocol to provide a default implementation of a requirement, a protocol can also be extended to add new functionality to any type that adopts that protocol. This is some really powerful stuff. For example, enumerations with a raw value type implicitly adopt the raw representable protocol. So to add functionality to all enumerations that have an integer raw value type, I'll extend RawRepresentable. I'm using a where clause to constrain the extension to types that adopt raw representable, who's RawValue is an integer type.
I'll go into using constraints like this in more depth in the next chapter. And in case you're wondering how I found out what types to use, here's a great quick reference site to look up types, protocols, operators, and global functions in the Swift standard library. I'll define a next offset method that attempts to retrieve the next case offset by one by default. While I'm at it, I'll also define a previous offset method too.
Now these methods will be available for any numeration with an integer raw value type. I'll define a number enumeration with a raw value type of an unsigned eight bit integer with five cases. Now I can call next or previous on any instance of this enumeration, optionally omitting the offset parameter. These methods return an optional, so I can also use optional binding such as to get the third case after two.
Previous works just as well. And nil is returned if I offset beyond the next or previous case.
- Adding source files, resources, links, and literals
- Adding pages to a playground
- Using overflow operators and bitwise operators
- Using ranges with strings
- Creating complex sequences
- Chaining higher-order functions
- Defining lazy properties
- Using failable initializers
- Mutating methods
- Working with singletons
- Nesting function types
- Creating error types and recursive enumerations
- Extending concrete types
- Referencing selectors and key paths
- Working with protocol-oriented programming
- Defining class-only protocols and optional protocols
- Using option sets, type checking, and casting operators
Skill Level Intermediate
Core Data for iOS and macOS Enterprise Developerswith Jon Bott1h 46m Intermediate
Distributing Your iOS App for Testing with TestFlightwith Ron Buencamino35m 10s Intermediate
iOS App Development: Test-Driven Developmentwith Harrison Ferrone2h 14m Intermediate
3. Strings and Characters
4. Sequences and Collections
5. Control Flow
6. Value and Reference Types
9. Access Control
Understand access control3m 47s
Next steps1m 2s
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.