Home » Swift » Requiring Protocol and Class in Swift Properties

Requiring Protocol and Class in Swift Properties

Posted by: admin November 30, 2017 Leave a comment

Questions:

In Objective-C you can require a class and additional protocol implementations for properties:

@property (nonatomic) UIViewController<UISplitViewDelegate> *viewController;

Is this possible in Swift? From the documentation it looks like you can only require either a class or a protocol.

Answers:

In Swift 4 you can do this by:

let viewController: UIViewController & UISplitViewDelegate

Questions:
Answers:

There are actually two of ways to achieve this in Swift:

  1. Using an empty “phantom” protocol. Create an empty protocol and make UIViewController conform to it. This is the most “Swift” method, it’s safe and it’s dynamic (doesn’t require specifying a class at compile-time).

    protocol _UIViewControllerType {}
    extension UIViewController: _UIViewControllerType {}
    
    class MyClass {
        weak var viewController: protocol<UISplitViewControllerDelegate, _UIViewControllerType>?
    }
    

    You can also declare a typealias for this type (just to reduce the code mess).

    class MyClass {
        typealias ViewControllerType = protocol<UISplitViewControllerDelegate, _UIViewControllerType>
        weak var viewController: ViewControllerType?
    }
    
  2. Using generic constraints. As mentioned by fnc12 and Konstantin Koval. This is safe, but doesn’t allow you to “swap” the view controller instance at run-time.

    class MyClass<T: UIViewController where T: UISplitViewControllerDelegate> {
        weak var viewController: T?
    }
    

I hope the next Swift release adds a way to specify both constraints without using a “phantom protocol”…

typealias ViewControllerType = UIViewController: UISplitViewControllerDelegate // wish

Questions:
Answers:

Yes you can do that

class A < T : SomeClass where T: Comparable> {
    var myProperty: T
    init(t :T) {
        myProperty = t
    }
}

Declare class A that has a property of type T. T is SomeClass or subclass and it has to adopt Comparable protocol

When declaring a property you can use protocol as type

class MyClass {
    var nsobject: NSObjectProtocol
    init(object : NSObjectProtocol) {
        nsobject = object
    }
}

// Pure Swift
protocol RandomNumberGenerator {
}

class Dice {
    let generator: RandomNumberGenerator
    //specify many protocols
    let printer: protocol<Printable, NicePrintable> 
}

You can read documentation here

Questions:
Answers:

Same as @akashivskyy answer, using empty “phantom” protocol
But here i am doing it as separate class which implements that
protocol – MyViewController, which can be used as type for var
declaration. Which simplified in my implementaion.

@objc protocol MySplitViewControllerDelegate : NSObjectProtocol {
    func controllerTitle() -> String
    optional func mySplitView() // write delegates
}

class MyViewController: UIViewController, MySplitViewControllerDelegate {
    func controllerTitle() -> String {
        return ""
    }
}

class ViewController: UIViewController {

    private(set) weak var viewController: MyViewController?

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

Questions:
Answers:

This is where generics are needed.

@property (nonatomic) UIViewController<UISplitViewDelegate> *viewController;

Assume you want class called ‘MyClass” to have a property called ‘viewController’ with type UIViewController (or subclass) and that conforms UISplitViewDelegate protocol. In Swift your code will look like

class MyClass<T:UIViewController where T:UISplitViewControllerDelegate>:NSObject{
    var viewController:T?

    override init(){
        super.init()
        //..
    }

    //  etc..
}

Notice

class MyClass<T:UIViewController where T:UISplitViewControllerDelegate>:NSObject

line. Here you specify a random T type but you also specify you want T to be derived from UIViewController and conform UISplitViewControllerDelegate. This condition will be checked during compilation. And you declare a property in this line

var viewController:T?

and specify its type as T.
And one question left – how to declare variable of type MyClass? I provide a minimum code from a sample project to illustrate more clearly.

class MyClass<T:UIViewController where T:UISplitViewControllerDelegate>:NSObject{
    var viewController:T?

    override init(){
        super.init()
        //..
    }

    //  etc..
}

class ViewController: UIViewController,UISplitViewControllerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        var a=MyClass <ViewController> ()
        a.viewController=self
        //..
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    //..
}

More information here

Good luck with generics.