Group Group Group Group Group Group Group Group Group

raywenderlich.com Forums

Introducing Protocol-Oriented Programming in Swift 3

Get started with protocol-oriented programming and protocol extensions in this introduction with Swift 3!


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/814-introducing-protocol-oriented-programming-in-swift-3

When I get to this portion of the tutorial, my assistant editor does not match yours.

To try it out, add the following to the bottom of the playground:
UnladenSwallow.african
You should see “I can fly!” appear in the assistant editor. But more notably, you just extended your own protocol!

When I enter UnladenSwallow.african on the bottom line, the sidebar displays “african”.

awesome article! thanks!

Hey dcdude,

If you download the playground from the tutorial at the end and examine it you’ll see what needed to be done.
In the section Extending Protocols, actually update that one line protocol Bird: CustomStringConvertible {,
then continue on with putting in the extension below it extension CustomStringConvertible where Self: Bird { var description: String { return canFly ? "I can fly" : "Guess I'll just sit here :[" } }

I think it’s just a case of needing slightly clearer instructions.

awesome article! thanks!

“protocol extensions do not introduce any additional state.”
I do not understand this description.
since protocol extension could add new var . new var is not additional state?

That was a lot of fun… I was a little confused by the (of:… and the (by:… syntax… are those swift keywords, or just arbitrary variable names? where can i find out more about of and by in that context?

is it possible, here, and in general, to have some problem/answer sets to help us learn these subjects better? maybe some programming assignments that utilize the learning material?

cheers!

-Dave

of: and by: are user defined argument labels, not swift keywords. Using prepositions is a new naming convention that is part of the Swift 3 design guidelines. You may want to check out the Style Guide for more details. (Another improvement in Swift 3 was making it so you can use almost any keyword as an argument label such as for: in: without needing backticks.

https://www.raywenderlich.com/148830/swift-style-guide-updated-for-swift-3

1 Like

Hi… I have just a little confusion here, I am doing something just bit different. Can you guys look into it please

In your case the Bird conforms to ConvertStringConvertible and then u create the extension of ConvertStringConvertible were u override the description property, in my case I have made the Bird conforms to ConvertStringConvertible but added the implementation of description in Bird’s extension rather than ConvertStringConvertible. So is there any advantage or disadvantage in either of the ways… kindly guide me.

protocol Bird: CustomStringConvertible
{
    var name: String { get }
    var canFly: Bool { get }
}

extension Bird
{
    var canFly: Bool
    {
        return (self is Flyable)
    }
    
    public var description: String
    {
        return canFly ? "I can fly" : "Guess I’ll just sit here :["
    }
}

Thank you for the awesome tutorial.
I have one questions regarding the error "Swift complains it cannot subscript a value of type [Racer] with an index of type CountableClosedRange. "
Why the error can be fixed when we replace concrete type ([Racer]) with a generic type(Sequence) ?
I tried " var a = racers[1…3] " in playground, it is working fine, no error happened.
But it gives error when I send “racer[1…3]” as parameter in the function.

UnladenSwallow enum, that is a funny joke

I, too, don’t understand this. Anyone care to explain?

Exactly the same thought occurred to me. My natural instinct was to do an extension for Bird:
extension Bird {
var description: String {
return canFly ? “I can fly” : “Guess I’ll just sit here :[”
}
}
Rather than an extension on CustomStringConvertible.
Any reason whether one is better than the other?

i understand 95%
but I not understand the last part.

extension Sequence where Iterator.Element == Racer {
  func topSpeed() -> Double {
    return self.max(by: { $0.speed < $1.speed })?.speed ?? 0
  }
}

why compilation fails if I replace Iterator.Element == Racer by Iterator.Element : Racer

for me the array is typed with Racer, and all member conform to Racer protocol, so Iterator.Element always equal with Racer, so I not understand why the :Racer in where statement not work.

Small clarity:

If I have a protocol with with a property of type UIViewController and I’ve wrote the default value for that property in the extension of the protocol as a computed property like below -

extension TelephoneHelper {

 var controller: UIViewController? {
    return self as? UIViewController
}

}

Now will be there any strong reference or retain cycle with the type(here UIViewController instance) which implements the TelephoneHelper.

This tutorial is more than six months old so questions regarding it are no longer supported for the moment. We will update it as soon as possible. Thank you! :]