Programming in Swift: Functions & Types

Jan 4 2022 · Swift 5.5, iOS 15, Xcode 13

Part 5: Protocols & Inheritance

44. Challenge: Protocols

Episode complete

Play next episode

Next
About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 43. Protocols & Extensions Next episode: 45. Value vs. Reference Types

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Notes: 44. Challenge: Protocols

Update Notes: This course was originally recorded in 2019. It has been reviewed and all content and materials updated as of October 2021.

Transcript: 44. Challenge: Protocols

This is your final challenge for the course. You’ll find instructions in the playground for this part of the course. Pause the video, and give it a try. Then, come back to see my solutions. Good luck!

Alright! First challenge. Create a protocol, Shape, that defines a read-only property area of type Double. Ok. A Shape protocol!

protocol Shape {

}

Then an area property, and “read-only” means it just needs a getter

  var area: Double { get }

Now, I need to implement Shape with structs representing Square, Triangle and Circle. The hints help you figure out how to get the area of each kind of shape, if you aren’t familiar with the math. So, to figure out the area of a square, we’d just need to know the length of one side.

struct Square: Shape {
  let side: Double
  
  var area: Double {
    side * side
  }
}

But, you know, we wrote that Numeric extension in the last episode. To use it here, we just need to copy it and paste it into this page.

extension Numeric {
  var squared: Self { self * self }
}

Now I can use it in this computed property!

side.squared

Next, a Triangle Shape.

struct Triangle: Shape {

}

To get the area, we first need a base and height.

  let base: Double
  let height: Double

And then we can calculate the area property.

  var area: Double  {
    0.5 * base * height
  }

Finally, a Circle Shape!

struct Circle: Shape {

}

For circles, we only need to know what the radius is to figure out the area.

  let radius: Double

The to get the area…

  var area: Double  {
  
  }

We multiply pi times the radius squared.

    .pi * radius.squared

And I was able to use Double’s pi property, there! Next, add a circle, a square and a triangle to an array. So, I’ll make one of each…

let square = Square(side: 4)
let triangle = Triangle(base: 3, height: 5)
let circle = Circle(radius: 2)

And then put them all in a Shape array.

let shapes: [Shape] = [square, triangle, circle]

The last part of this challenge was to convert the array of shapes to an array of their areas using map.

print(shapes.map { $0.area })

I also printed it out so we could see the results in the console!

Final challenge of the course! We’ve got a function that takes a Double and tells you if it is an integer by comparing the Double to a rounded version of itself.

Turn that function into a computed property of FloatingPoint! Ok, I’ll start by opening up an extension on FloatingPoint

extension FloatingPoint {

}

And then I’ll start the computed property. It’ll be a Bool, just like the function’s return type.

  var isInteger: Bool {
  
  }

Now to do the same calculation, I’ll need to use self, again.

    rounded() == self

But I don’t actually need it in front of rounded! It’s assumed I’m calling it on self, just like that. Now! Try the property out on a Double instance and Float instance. I’ll make those instances, and specify the types, because otherwise Swift would assume they’re both Doubles.

let double: Double = 5.0
let float: Float = 3.7

And then I can find out which ones are integers!

double.isInteger
float.isInteger

Just the 5! It turns out 3.7 isn’t an integer.