Swift: Protocols
Please find the GitHub link of all the code here.
Protocols are used to create the loosely coupled system. With protocols, we define the contracts based on which two layers or components of the system can communicate with each other.
Loose coupling is actually implemented by avoiding the composition of concrete objects. Instead, the composition is done with help of abstract types.
Now let's create a protocol with the associated types. This helps us create protocols with generic types.
protocol Employable {
associatedtype Item
var firstName: Item {get set}
var fullName: Item {get}
}
In the example above Item is the associated type (Generic) whose concrete type would be defined later.
Implementation of the protocol with String being the concrete type.
class Employee: Employable {
//Concrete type defined
typealias Item = String
//Properties
var reportees = Array<String>()
var firstName: String
var fullName: String {
return "Mr. " + firstName
}
init(_ name: String) {
firstName = name
}
}
protocol EmployeeProto: Employable {
func getSalary() -> Int
}
typealias MultipleProtocols = Employable & Deployable
if let employee = employee as? MultipleProtocols
protocol someClassOnly: AnyObject {
func addToList(_ item: String)
}
领英推荐
extension someClassOnly {
func addToList(_ item: String) {
print("Default Implementation of addToList")
}
}
@objc protocol Countable {
@objc optional func displayCount()
}
Implementing Equatable protocol: We need to implement an equatable protocol in order to define how two objects need to be equated.
struct Car: Equatable {
? ? let make: String
? ? static func == (lhs: Car, rhs: Car) -> Bool {
? ? ? ? return lhs.make == rhs.make
? ? }
}
Implementing Comparable Protocol: We need to implement a comparable protocol in order to define how two objects need to be compared.
struct Student: Comparable {
? ? static func < (lhs: Student, rhs: Student) -> Bool {
? ? ? ? return lhs.marks < rhs.marks
? ? }
?? ?
? ? let name: String
? ? let marks: Double
}
Implementing Hashable Protocol: Defines how an object can be stored as a Key-Value pairing.
struct Person
let name: String
}
struct Account: Hashable {
static func == (lhs: Account, rhs: Account) -> Bool {
return lhs.account.name == rhs.account.name
}
func hash(into hasher: inout Hasher) {
hasher.combine(account.name)
}
let account: Person
}
Opaque Types: Some concrete implementations would be returned from this function at the time of execution. Returning with the keyword "some" is known as opaque type.
Implementing Sequence and Iterator Protocols: To create your own sequence.
struct CountDown: Sequence {
let start: Int
//some
func makeIterator() -> some IteratorProtocol {
MyIterator(self)
}
}
struct MyIterator: IteratorProtocol {
let countdown: CountDown
var currentValue: Int
init(_ cntd: CountDown) {
countdown = cntd
currentValue = countdown.start
}
//next - Needs to be implemented
mutating func next() -> Int? {
currentValue -= 1
return currentValue >= 0 ? currentValue : nil
}
}
func demo()
? ? let coutDown = CountDown(start: 10)
? ? for count in coutDown {
? ? ? ? print(count)
? ? }
}