Home > Software design >  IOS Swift - Removing a inherited property from a class when implementing from SDK
IOS Swift - Removing a inherited property from a class when implementing from SDK

Time:03-10

I am attempting to use a Default Layout View Controller from a Software Developer Kit. This Default Layout contains elements of a generic UI which can be used for quick deployment and still allowing full functionality of the kits purpose. I am attempting to learn how to exclude certain members of a class I pull from this kit. I am more conceptually trying to understand how I would go about doing this.

Lets say this view controller class I am using has 4 class members that are properties (among other members that are methods, other properties)

Class: DefaultLayoutViewController

Properties:

  • topViewController
  • bottomViewController
  • leftViewController
  • rightViewController

I wish to remove (or hide, disable, etc.) the bottomViewController property, as its features have been replaced with other UI elements I created.

import SomeSDK    

class MainViewController: DefaultLayoutViewController {

//Code for UI elements I created

}

The above code pulls all of the elements from the SDK's Class DefaultLayoutViewController, and builds my elements on top of it.

This property has this information about it in the documentation.

@property (nullable, nonatomic) SDKbottomViewController *bottomViewController

I have attempted to override from within the MainViewController class, but I am told 'Cannot override with a stored property'. How would I go about removing this inherited property from the class I am referencing? I obviously cannot simply delete the property from the initial class since I'm installing it in from an SDK Pod. I am newer to Swift, and coding in general, so perhaps I am just not wording my questions correctly in my research and failing to find an obvious solution that is documented out there. If this is repeated anywhere that I have missed, my apologies.

CodePudding user response:

There is no way for a subclass to remove a property from a superclass. This violates the meaning of "inheritance." We usually refer to this as the Liskov Substitution Principle. There are various subtleties about how (and even if) this principle applies, but the way most programmers apply this is that anywhere DefaultLayoutViewController can be used, it is required that MainViewController also be usable. Consider this function:

func handleController(controller: DefaultLayoutViewController) {
    doSomething(with: controller.bottomViewController)
}

If this were called with an instance of MainViewController, after you've "removed" bottomViewController, what should happen? This isn't a meaningful thing to do.

How you should handle this depends on exactly what you're trying to achieve. Generally the answer is "don't use class inheritance; use protocols." That may not be easy if you're tied to an existing class-based framework, but it doesn't change the fact that removing a property is not possible with inheritance.

  • Related