April 2026
Intermediate
683 pages
25h 29m
English
Just like imposing certain functions, protocols also can impose certain initializer signatures. All implementing data types will have to conform to that signature.
Listing 10.6 demonstrates such an example. The Notification protocol has an initializer requirement, by which init needs to be implemented with two parameters: recipient and message.
protocol Notification { init(recipient: String, message: String) func send()}struct Email: Notification { var target_address: String var mail_body: String init(recipient: String, message: String) { self.target_address = recipient self.mail_body = message } func send() { print("Email sent to \(self.target_address)") }}struct Sms: Notification { var target_phone: String ...
Read now
Unlock full access