I use Swift 3 to demonstrate the object-oriented programming (OOP) characteristic of "polymorphism." Polymorphism enables you to develop general code that works with groups of related classes instead of developing code for each individual class.
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
OOP Polymorphism.xcodeproj
OOP Polymorphism
.gitignore
LICENSE.md
README.md

README.md

OOP Polymorphism

In this Xcode 8.2.1 project built against the iOS 10 SDK, I used Swift 3 to demonstrate the object-oriented programming (OOP) characteristic of "polymorphism," which I've discussed extensively in the articles here and here.

In one example of polymorphism, I created an outlet collection (IBOutletCollection in Objective-C) and connected the UITextField, UISlider, UISegmentedControl, and UIStepper to that same outlet collection:

@IBOutlet var multipleOutlets : [UIControl]!

Y'all should know what an outlet is by now. You can watch a video showing me creating the Swift outlet collection discussed above.

I then wired up an IBAction that iterates over each UIControl descendant reference stored in the "multipleOutlets" variable and sets their common isEnabled property to "true" or "false." You can see how the IBAction in this project works by watching this video.

In another example -- an exercise in OOP design and development -- I created a simple hierarchical taxonomy of three classes composed of one parent and two children. I used an information/computer network as an example of a real-world system to model in software.

The parent class is the blueprint for the child classes. In other words, it defines some general properties and functionality that would be expected of any information network. The child classes, one modelling a Wi-Fi network, and the other modelling a cellular network, each override the behavior of the parent. They provide functionality specific to the network types they are modelling.

I then demonstrated how to declare a variable of the parent type and assign it references to its child classes. This is an example of late binding in which the parent's behavior is determined at runtime, not at compile time, and thus demonstrates polymorphism. To be more precise, I should use the term "dynamic dispatch", as Apple does in its Swift documentation, to describe this behavior where "the program has to determine at runtime which method or property is being referred to" when discussing polymophism in Swift (and Objective-C). You can see how I use the parent variable to manipulate child instances by watching this video. Here's a peek at the class hierarchy:

// the base class for networking
class IBInformationNetwork
{
...
}

// a child for Wi-Fi networking, extending the base
// class for basic/common networking
class IBWifiNetwork: IBInformationNetwork
{
...
}

// a child for cellular networking, extending the base
// class for basic/common networking
class IBCellularNetwork: IBInformationNetwork
{
...
}

Ands here's a polymorphic variable demonstrating late binding or dynamic dispatch:

var informationNetwork : IBInformationNetwork =
    IBInformationNetwork(providerName: "Acme, Inc.", bandwidth: 150, speed: 15)
...

class FirstViewController: UIViewController
{
...
    var wifiNetwork : IBWifiNetwork =
        IBWifiNetwork(providerName: "WiFi, Inc.", bandwidth: 100, speed: 10)
...
        informationNetwork = wifiNetwork
...
}

...

class SecondViewController: UIViewController
{
...
    var cellularNetwork : IBCellularNetwork =
        IBCellularNetwork(providerName: "Phone Co, Inc.", bandwidth: 10, speed: 1)
...
        informationNetwork = cellularNetwork
...
}

Polymorphism enables you to develop general code that works with groups of related classes instead of developing code for each individual class.

Xcode 8.2.1 project settings

To get this project running on the Simulator or a physical device (iPhone, iPad), go to the following locations in Xcode and make the suggested changes:

  1. Project Navigator -> [Project Name] -> Targets List -> TARGETS -> [Target Name] -> General -> Signing
  • Tick the "Automatically manage signing" box
  • Select a valid name from the "Team" dropdown
  1. Project Navigator -> [Project Name] -> Targets List -> TARGETS -> [Target Name] -> General -> Identity
  • Change the "com.yourDomainNameHere" portion of the value in the "Bundle Identifier" text field to your real reverse domain name (i.e., "com.yourRealDomainName.Project-Name").