Xcode warns of missing protocol definition, although @protocol is used

Since I recently had an import loop, I move all #import tags (related to my own files) from the header to the corresponding .m file. I also added @class and @protocol forward-declarations to calm the compiler. However, I still warn:

 Cannot find the protocol definition for 'MyCustomDelegate'. 

As I said, there is @protocol MyCustomDelegate before using it in @interface -Block. Interestingly, this warning only occurs if the corresponding delegate is declared in another file (whose header is imported into the .m file).

I read that one solution is to declare the delegate in a separate header file and import this file directly into the header of the class that the delegate implements. Is this really the way to go? Are there any other solutions? I think that these delegates have already inflated our code enough, now I have to continue and even declare my own file for it?

A small code example to better illustrate the problem:

NewFooController.h

 #import <UIKit/UIKit.h> @protocol NewFooControllerDelegate; @interface NewFooController : UITableViewController @property (nonatomic, weak) id<NewFooControllerDelegate> delegate; @end @protocol NewFooControllerDelegate @end 

HomeTableViewController.h

 #import <UIKit/UIKit.h> @protocol NewFooControllerDelegate; // warning points to line below @interface HomeTableViewController : UITableViewController <NewFooControllerDelegate> @end 

HomeTableViewController.m

 #import "HomeTableViewController.h" #import "NewFooController.h" @implementation HomeTableViewController @end 
+6
source share
2 answers

HomeTableViewController.h refers to the protocol, but it has not yet been declared. If you import NewTaskController.h into HomeTableViewController.h before it tries to use it, it should solve your problem.

Of course, you can remove the import from HomeTableViewController.m.

-1
source

Not sure if this is the โ€œbest wayโ€, but try importing the class header that implements the protocol before the class header file.

HomeTableViewController.m

 #import "NewFooController.h" #import "HomeTableViewController.h" @implementation HomeTableViewController @end 

And you can remove the protocol declaration in HomeTableViewController.h

 #import <UIKit/UIKit.h> @interface HomeTableViewController : UITableViewController <NewFooControllerDelegate> @end 
-1
source

Source: https://habr.com/ru/post/912307/


All Articles