Group Group Group Group Group Group Group Group Group

raywenderlich.com Forums

Introducing Protocol-Oriented Programming in Swift 2

Get started with protocol-oriented programming and protocol extensions in this introduction to the latest exciting feature in Swift 2!


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

so for this line:

func topSpeed<T:  CollectionType where T.Generator: Flyable>(collection: T) -> Double {
collection.map { $0.airspeedVelocity }.reduce(0) { max($0, $1) }

I get this error:

error: cannot convert value of type '(_) -> _' to expected argument type '_ -> _'
collection.map { $0.airspeedVelocity }.reduce(0) { max($0, $1) }

I’m not sure how to fix that. I’m using swift 2.2/ Xcode 7.3 (7D715)

I like Blue :slight_smile:

ok I figured out my typo :blush::

func topSpeed<T: CollectionType where T.Generator.Element == Flyable>(c: T) -> Double {
return c.map { $0.airspeedVelocity }.reduce(0) { max($0, $1) }
}

works :angel: :-X

1 Like

Code update for Swift 3.

In general, CollectionType is now Collection.

This is the code to make the Collection extension work:

extension Collection {
    func skip(skip: Int) -> [Generator.Element] {
        guard skip != 0 else { return [] }
    
    var index = self.startIndex
    var result: [Generator.Element] = []
    var i = 0
    repeat {
        if i % skip == 0 {
            result.append(self[index])
        }
        
        index = self.index(after: index)
        i += 1
    } while (index != self.endIndex)
    return result
}

}

Also the part where you are making Bird conform to BooleanType doesn’t work. I could find a fix as both Bool and BooleanLiteralType aren’t protocols.

Hi . do you remember NSRegularExpression Tutorial: Getting Started,

I updated to Swift 4 version.

Here is GitHub repo https://github.com/BoxDengJZ/Swift4-NSRegularExpression-Tutorial-Getting-Started.

If it is of use, a star is enough.

@aestus

This tutorial is more than six months old so questions are no longer supported at the moment for it. Thank you!

1 Like