Swift 3 ObjectOriented Programming - Second Edition by Hillar Gaston C
Author:Hillar, Gaston C.
Language: eng
Format: epub
Publisher: Packt Publishing
Published: 2017-02-26T16:00:00+00:00
Tip
Protocols in Swift allow us to make sure that the classes that implement them define all the members specified in the protocol. If they don't, the code won't compile.
In this case, the ComicCharacter protocol didn't specify any initializer requirements, so each class that conforms to the protocol can define its initializer without any constraint. AngryDog and AngryCat declare initializers with a different number of arguments.
Taking advantage of the multiple inheritance of protocols
Swift doesn't allow us to declare a class with multiple base classes or superclasses, so there is no support for multiple inheritance of classes. A subclass can inherit just from one class. However, a class can conform to one or more protocols. In addition, we can declare classes that inherit from a superclass and conform to one or more protocols. Thus, we can combine class-based inheritance with protocols.
We want the AngryCat class to conform to both the ComicCharacter and GameCharacter protocols. Thus, we want to use any AngryCat instance as both a comic character and a game character. In order to do so, we must change the class declaration and add the GameCharacter protocol to the list of protocols that the class conforms to and declare all the members included in this added protocol within the class.
The following lines show the new class declaration that specifies that the AngryCat class conforms to both the ComicCharacter and GameCharacter protocols. The code file for the sample is included in the swift_3_oop_chapter_05_07 folder:
open class AngryCat: ComicCharacter, GameCharacter {
After changing the class declaration, the Playground execution will fail because the AngryCat class doesn't implement the members required by the GameCharacter protocol. The Swift compiler generates the following errors and notes:
error: type 'AngryCat' does not conform to protocol 'GameCharacter' open class AngryCat: ComicCharacter, GameCharacter { ^ note: protocol requires property 'fullName' with type 'String'; do you want to add a stub? var fullName: String { get set } ^ note: protocol requires property 'score' with type 'UInt'; do you want to add a stub? var score: UInt { get set } ^ note: protocol requires property 'x' with type 'UInt'; do you want to add a stub? var x: UInt { get set } ^ note: protocol requires property 'y' with type 'UInt'; do you want to add a stub? var y: UInt { get set } ^ note: protocol requires function 'drawAt(x:y:)' with type '(UInt, UInt) -> ()'; do you want to add a stub? func drawAt(x: UInt, y: UInt) ^ note: protocol requires function 'moveTo(x:y:)' with type '(UInt, UInt) -> ()'; do you want to add a stub? func moveTo(x: UInt, y: UInt) ^ note: protocol requires function 'intersects(character:)' with type '(GameCharacter) -> Bool'; do you want to add a stub? func intersects(character: GameCharacter) -> Bool ^
We have to add the following lines to the body of the AngryCat class to implement the stored properties specified in the GameCharacter protocol. The code file for the sample is included in the swift_3_oop_chapter_05_08 folder:
open var score: UInt = 0 open var fullName: String =
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Secrets of the JavaScript Ninja by John Resig & Bear Bibeault(6554)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6445)
Kotlin in Action by Dmitry Jemerov(5092)
Odoo 15 Development Essentials - Fifth Edition by Daniel Reis & Greg Mader(3511)
Odoo 15 Development Essentials by Daniel Reis(2837)
React Native - Building Mobile Apps with JavaScript by Novick Vladimir(2551)
Learning Angular - Second Edition by Christoffer Noring(2381)
Pride and Prejudice by Jane Austen(2370)
Mobile Forensics Cookbook by Igor Mikhaylov(2040)
Computers For Seniors For Dummies by Nancy C. Muir(2023)
Bulletproof Android: Practical Advice for Building Secure Apps (Developer's Library) by Godfrey Nolan(1893)
Android Development with Kotlin by Marcin Moskala & Igor Wojda(1817)
Building Android UIs with Custom Views by Raimon Ràfols Montané(1810)
1936941139 (N) by Bob Rosenthal(1757)
Building Progressive Web Apps: Bringing the Power of Native to the Browser by Ater Tal(1748)
Hands-On Internet of Things with MQTT by Tim Pulver(1731)
Android App Development by Franceschi Hervé J.;(1729)
Ember.js in Action by Joachim Haagen Skeie(1711)
Hands-On Design Patterns with React Native by Mateusz Grzesiukiewicz(1670)
