O'Reilly logo

Swift Functional Programming - Second Edition by Dr. Fatih Nayebi

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Polymorphism

Polymorphism means many forms. In general, the ability to take more than one form is called polymorphism. In an OOP language such as Swift, a polymorphic reference is one that can, over time, refer to instances of more than one class. Let's examine an example of the iOS SDK, UIView.

There are lots of UIView subclasses, including the following:

  • UILabel
  • UITextField
  • UIButton

We can declare a view that can take many forms, such as the following:

var view: UIView view = UIButton() view = UILabel() view = UITextField() 

Polymorphism allows us to write a more generic code that works with families of objects rather than writing code for a specific class.

In this example, regardless of which class we initiate, we can access all the ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required