0

Suppose I have one table view controller (controlling a static table view) and another regular view controller.

I want to add a common property to both of them. The first thing came in my mind is subclassing, meaning let these two controllers derive from a common abstract super class. After pondering a bit, I recall protocol can also achieve this.

My questions is, which approach will be the correct practice, or there are better practices?

David Liu
  • 16,374
  • 12
  • 37
  • 38

1 Answers1

1

Subclassing is likely the correct approach. Protocols don't add properties automatically, they only dictate that if your class conforms to a specific one that the class implements them. If your coming from the Java world then an interface would be the equivalent.

A category might be appropriate if you want to add common functionality (methods) to all instance of a class, such as a UITableview controller. The downside is that you can't declare additional instance variables (or properties) via a category (well technically you can via associated objects, but that's another rabbit hole).

Brandon
  • 2,387
  • 13
  • 25