0

I am trying to define my protocol so that the class implementing it has to also be a UIViewController.

So i typed the following code into a header file:

#import <UIKit/UIKit.h>
#import <UIKit/UIViewController.h>
#import <EventKit/EventKit.h>
#import <EventKitUI/EventKitUI.h>

@protocol MySuperProtocol<UIViewController> // <-- here

@property(nonatomic, weak, nullable) id<EKEventViewDelegate> delegate;

@end

I always get the error saying that:

Cannot find protocol declaration for 'UIViewController'

If i replace UIViewController with NSObject, code compiles. If i remove the <> inheritance after the protocol, code compiles.

I tried all combinations of

#import <UIKit/UIKit.h>
#import <UIKit/UIViewController.h>

to no avail.

What am i doing wrong?

1 Answer 1

1

You are misunderstanding the syntax. @protocol MySuperProtocol<UIViewController> doesn't establish a constraint that implementers of MySuperProtocol must be UIViewControllers. It says that MySuperProtocol conforms to UIViewController

However, unlike NSObject, UIViewController is not a protocol; it is a class. A protocol cannot conform to a class, only to another protocol.

You can refer to the documentation:

Protocols inherit from other protocols

In the same way that an Objective-C class can inherit from a superclass, you can also specify that one protocol conforms to another.

There is no way to constrain protocol adoption in Objective C.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.