keithbhunter keithbhunter - 1 year ago 145
iOS Question

Swift Property that conforms to a Protocol and Class

@property (strong, nonatomic) UIViewController<UITableViewDelegate> *thing;

I want to implement a property like in this Objective-C code in Swift. So here is what I've tried:

class AClass<T: UIViewController where T: UITableViewDelegate>: UIViewController {
var thing: T!

This compiles. My problem comes when I add properties from the storyboard. The
tag generates an compiler error.

class AClass<T: UIViewController where T: UITableViewDelegate>: UIViewController {
@IBOutlet weak var anotherThing: UILabel! // error
var thing: T!

The error:

Variable in a generic class cannot be represented in Objective-C

Am I implementing this right? What can I do to fix or get around this error?

Update - 5/25/16

A proposal for Swift 3 is to replace
Protocol<P1, P2>
Any<P1, P2>
. This could open up the door to encapsulating a type that conforms to a protocol and a class, like
Any<UIViewController, UITextFieldDelegate>
. This is just my own speculation at this point. We will have to wait to see when Swift 3 comes. For those interested, here is the proposal.

Answer Source

I came across the same issue, and also tried the generic approach. Eventually the generic approach broke the entire design.

After re-thinking about this issue, I found that a protocol which cannot be used to fully specify a type (in other words, must come with additional type information such as a class type) is unlikely to be a complete one. Moreover, although the Objc style of declaring ClassType<ProtocolType> comes handy, it disregards the benefit of abstraction provided by protocol because such protocol does not really raise the abstraction level. Further, if such declaration appears at multiple places, it has to be duplicated. Even worse, if multiple declarations of such type are interrelated (possibly a single object will be passed around them ), the programme becomes fragile and hard to maintain because later if the declaration at one place needs to be changed, all the related declarations have to be changed as well.


If the use case of a property involves both a protocol (say ProtocolX) and some aspects of a class (say ClassX), the following approach could be taken into account:

  1. Declare an additional protocol that inherits from ProtocolX with the added method/property requirements which ClassX automatically satisfy. Like the example below, a method and a property are the additional requirements, both of which UIViewController automatically satisfy.

    protocol CustomTableViewDelegate: UITableViewDelegate {
        var navigationController: UINavigationController? { get }
        func performSegueWithIdentifier(identifier: String, sender: AnyObject?)
  2. Declare an additional protocol that inherits from ProtocolX with an additional read-only property of the type ClassX. Not only does this approach allow the use of ClassX in its entirety, but also exhibits the flexibility of not requiring an implementation to subclass ClassX. For example:

    protocol CustomTableViewDelegate: UITableViewDelegate {
        var viewController: UIViewController { get }
    // Implementation A
    class CustomViewController: UIViewController, UITableViewDelegate {
        var viewController: UIViewController { return self }
        ... // Other important implementation
    // Implementation B
    class CustomClass: UITableViewDelegate {
        private var _aViewControllerRef: UIViewController // Could come from anywhere e.g. initializer
        var viewController: UIViewController { return _aViewControllerRef } 
        ... // UITableViewDelegate methods implementation

PS. The snippet above are for demonstration only, mixing UIViewController and UITableViewDelegate together is not recommended.