Other useful protocols

While learning all of the Swift standard library isn’t vital to your success as a Swift

developer, there are a few other important protocols you’ll find useful in almost any project.

Hashable

The Hashable protocol, a subprotocol of Equatable, is a requirement for any type you want to use as a key to a Dictionary:

protocol Hashable : Equatable { var hashValue: Int { get }

}

Hash values help you quickly find elements in a collection. In order for this to work, values that are considered equal by == must also have the same hash value.

Because the number of hash values is limited, there’s a finite probability that non- equal values can have the same hash.

The mathematics behind hash values are quite complex, but the important thing to keep in mind is that equal values must have equal hash values. Most Swift types adopt Hashable, so you can usually rely on hash values of properties to build your hash value.

For example:

class Student {

let email: String var firstName: String var lastName: String

init(email: String, firstName: String, lastName: String) { self.email = email

self.firstName = firstName self.lastName = lastName

}

}

extension Student: Equatable {

static func ==(lhs: Student, rhs: Student) -> Bool { return lhs.email == rhs.email

}

}

extension Student: Hashable { var hashValue: Int {

return email.hashValue

}

}

In the code above, you assign students an email address when they enroll and use this email address to uniquely identify the student. Therefore, student objects are considered equal (meaning they describe the same student) if they have the same email address. Because of this, the hash value for a Student must be based on the student’s email address. Since String conforms to Hashable, you can simply return

the hash value of email instead of having to calculate a hash yourself. You can now use the Student type as the key in a Dictionary:

let john = Student(email: "[email protected]", firstName: "Johnny", lastName: "Appleseed")

let lockerMap = [john: "14B"]

CustomStringConvertible

The CustomStringConvertible protocol can help log and debug your objects; it’s a handy protocol to keep in your toolbox.

When you call print() on an object such as Student, Swift prints a not-so- informative description of the object:

print(john)

// Student

As if you didn’t already know that! :] The CustomStringConvertible protocol has only a description property requirement. This property customizes how the object appears in print() statements and in the debugger:

protocol CustomStringConvertible { var description: String { get }

}

By adopting CustomStringConvertible on the Student type, you can provide a more readable representation:

extension Student: CustomStringConvertible { var description: String {

return "(firstName) (lastName)"

}

}

print(john)

// Johnny Appleseed

CustomDebugStringConvertible is similar to CustomStringConvertible; it behaves exactly like CustomStringConvertible except it also defines a debugDescription. Use CustomDebugStringConvertible along with debugPrint() to print to the output only in debug configurations.

results matching ""

    No results matching ""