We want to create a shortcut to access the members of the party. Subscripts are very useful to generate shortcuts to access the members of any array, collection, list, or sequence. Subscripts can define getter and/or setter methods, which receive the argument specified in the subscript declaration. In this case, we will add a read-only subscript to allow us to retrieve a member of the party through its index value indicated within square brackets. Thus, the subscript will only define a getter method.
We will use UInt
as the type for the index argument because we don't want negative integer values, and the getter for the subscript will return an optional type. In case the index value received is an invalid value, the getter will return none
.
First, we will add the following line to the PartyProtocol
protocol body. The code file for the sample is included in the swift_3_oop_chapter_06_09
folder:
subscript(index: UInt) -> MemberType? { get }
We included the subscript
keyword followed by the argument name and its required type--which is the returned type, MemberType?
--and the requirement for just a getter method, get
. The requirements for the getter and/or setter methods are included with the same syntax we used for properties' requirements in protocols. Remember that MemberType
is the associated type we added to the PartyProtocol
protocol.
Now, we have to add the code that implements the previously defined subscript in the Party<AnimalElement>
class. We must add the following code after the open class Party<AnimalElement: AnimalProtocol>: PartyProtocol where AnimalElement: Equatable {
line that starts the declaration of the Party<AnimalElement>
class that we want to edit to make it conform to the changes in the PartyProtocol
protocol. The code file for the sample is included in the swift_3_oop_chapter_06_09
folder:
open subscript(index: UInt) -> AnimalElement? { get { if (index <= UInt(members.count - 1)) { return members[Int(index)] } else { return AnimalElement?.none } } }
After making the preceding changes, we can specify an UInt
value enclosed in square brackets after an instance of Party<AnimalElement>
to retrieve an instance of AnimalElement
--specifically AnimalElement?
--from the party. The following lines show examples of its usage with the Party<Lion>
instance named lionsParty
. The first two lines retrieve a Lion
instance and print the value for its name
property because the array has a member both at index 0
and index 1
. However, the array doesn't have a member at index 50
, so the else condition will be executed in this case. The code file for the sample is included in the swift_3_oop_chapter_06_09
folder:
if let lion = lionsParty[0] { print(lion.name) } if let lion = lionsParty[1] { print(lion.name) } if let lion = lionsParty[50] { print(lion.name) } else { print("There is no lion with that index value") }
The following lines show the output generated in the Playground after making the changes to the PartyProtocol
protocol and the Party<AnimalElement>
class and executing the preceding code:
Simba Nala There is no lion with that index value
The following screenshot shows the Playground with the execution results:
3.149.234.118