Swift 3, function in protocol that return object of class that conforms to a protocol

D. Mika

I've read so many articles about classes and protocols but still didn't found a solution. May be I just didn't understand all of them.

So there's a simple protocol (removed some parts not relevant to the problem):

protocol InfoObject : NSObjectProtocol
{
    var statusImage: UIImage? {get}

    func viewControllerForItemIndex(_ index: Int?) -> UIViewController?
}

But I would like the function viewControllerForItemIndex not only return an object of type UIViewController? but it should conform to the protocol InfoObjectDisplaying (not shown here) as well.

In Objective-C I would write:

- (UIViewController<InfoObjectDisplaying>*)viewControllerForItemIndex:(NSInteger) index;

Is this possible in swift?

Pedro Castilho

First off, you don't need to inherit from NSObjectProtocol in Swift :)

And no, you can't do this directly in Swift. You can require that an object conforms to a protocol or a class, but not both. However, you can declare viewControllerForItemIndex as a generic function and require that its type parameter conforms to specific requirements:

func viewControllerForItemIndex<T: UIViewController where T: InfoObjectDisplaying>(_ index: Int?) -> T?

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Protocol Extension in Swift Where Object Is a Class and conforms to a Protocol

Swift Property that conforms to a Protocol and Class

In swift, how do I return an object of the same type that conforms to a protocol

Checking if a Swift class conforms to a protocol and implements an optional function?

Swift 3. Cast Any to class which conforms specific protocol

Swift function with a parameter which conforms to a protocol

Swift extension that conforms to protocol

Extend a Class that conforms a protocol?

Check if class conforms to protocol

Create object of Objective-C class at runtime in Swift, which conforms to Objective-C protocol

Swift: AnyClass variable that conforms to protocol

"Type of a class which conforms to a protocol" as parameter in a method swift

Swift extension of a class ONLY when it conforms to a specific protocol

Swift 4+ instantiating a class variable that conforms to a protocol

Swift init from unknown class which conforms to protocol

Variable of type that conforms to protocol and class used in generic function

Protocol function no working in swift 3

Swift 3: Is there a way to cast an object to a class and protocol at the same time?

Checking if Any.Type conforms to a protocol in Swift

Order of cases in an enum that conforms to CaseIterable protocol in Swift?

Declare metatype that is a subclass and conforms to a protocol in Swift 4

Heterogeneous array that conforms to protocol with associated type in Swift

Swift: Does Array conforms to Sequence Protocol?

Passing a Class which conforms to a protocol as an argument

How to pass a class type, that conforms to protocol

Custom method on class that conforms to MKAnnotation protocol

Using decodeObject(of: forKey:) to decode an object that conforms to a protocol

Is a blank function conventional in subclass that conforms to custom protocol?

Variable that conforms to a protocol that has a generic function