Denis
Denis

Reputation: 417

Creating an object instance that conforms to a protocol

I've created a protocol (LEService) that I will conform to in multiple UIViewControllers. I won't know until runtime which UIViewController service has been chosen.

Is there anyway to create an instance of an object that conforms to a protocol without saying what the object is until runtime?

Upvotes: 2

Views: 114

Answers (1)

Michael Robinson
Michael Robinson

Reputation: 29498

Sure, you can do it like this:

id <LEService> objectName = [self returnObjectConformingToLEServiceProtocol];

id is Objective C's generic type.

Upvotes: 3

Related Questions