Intermediate Swift 3 - Part 8: Protocols | Ray Wenderlich

In this Intermediate Swift 3 video tutorial you'll learn the basics of grouping behavior through protocols and how to implement them with extensions.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/3536-intermediate-swift-3/lessons/8

Hi! When you download the challenge for Protocols… the title says Lesson 7: Initializers (it’s just a tad confusing).

I was also confused by this. Still an issue.

ya these download materials are always confusing from past couple of lessons. Still same i

Hi Brian,

I got one doubt regarding the Truncating Division which is mentioned in the Apple’s Documentation of the Swift Protocol.

In the documentation,there’s this example of generating a random number:

class LinearCongruentialGenerator: RandomNumberGenerator {

var lastRandom = 42.0
let m = 139968.0
let a = 3877.0
let c = 29573.0
func random() -> Double {
    lastRandom = ((lastRandom * a + c).**truncatingRemainder**(dividingBy:m))
    return lastRandom / m
}

}
let generator = LinearCongruentialGenerator()
print(“Here’s a random number: (generator.random())”)
// Prints “Here’s a random number: 0.37464991998171”

print(“And another one: (generator.random())”)
// Prints "And another one: 0.729023776863283

I am really not understanding the difference between a Truncating Remainder and a normal Remainder and how the random number is generated.

Thanks,
Ashwin Shelke.

Is there a way when iterating through the vehicles array to get other parameters from the object not in the protocol? I can see using this technique for example in a tableview showing a list of all the vehicles (even though they are different objects). But if the user taps on the cell with the vehicle could they then change one of its non protocol parameters?
Thanks, Mike

yes. you just need to add an if statement to check if its the type of model object you’re looking for. lets say that you are looking for a Car in the vehicle array. It would look like this:

for vehicle in vehicles {
    if let car = vehicle as? Car {
    //add implementation here
    }
}