AppDelegate class not found

I imported AppDelegate.h in many classes using:

#import "AppDelegate.h" @interface LoginViewController : UIViewController<UITextFieldDelegate> { } @property (nonatomic, retain) AppDelegate *app; 

but for some reason it stopped working in my loginviewcontroller.h. It says:

  unknown type name 'AppDelegate' [1] property with 'retain (or strong)' attribute must be of object type [3] 

I made this class at the beginning, and it always worked as it should. I did not make any changes to the class or AppDelegate when it started with this error.

I can import it into other classes without problems. I also tried to recreate the class, but that didn't help.

Has anyone figured out how to solve this strange error?

+4
source share
2 answers

Using this line of code is not recommended.

 @property (nonatomic, retain) AppDelegate *app; 

in every classroom you need. An easy way to access the delegate application where you need it is this:

 AppDelegate* appDel = (AppDelegate*)[[UIApplication sharedApplication] delegate]; 

obviously you need to do:

 #import "AppDelegate.h" 

in the class in which you use it.

If you need a cleaner way to do this, you can create a class method in AppDelegate.h as follows:

 +(AppDelegate*)sharedAppdelegate; 

in AppDelegate.m is defined as follows:

 +(AppDelegate*)sharedAppdelegate { return (AppDelegate*)[[UIApplication sharedApplication] delegate]; } 

Then, where you need it, you can just call (after importing AppDelegate.h):

 AppDelegate* sharedApp = [AppDelegate sharedAppdelegate]; 

Hope this helps.

PS Why do you need access to a delegate?

+14
source

Declare a direct link in the .h file

 @class AppDelegate @interface LoginViewController : UIViewController<UITextFieldDelegate> { } 

// save it as an assignment, not save, so that keepCount is aligned for the variable

 @property (nonatomic, assign) AppDelegate *app; 

in the .m file, take a pointer to Appdelegate by importing AppDelegate.h and then assigning a variable

 #import "AppDelegate.h" - (void)viewDidLoad { self.app = (AppDelegate*)[[UIApplication sharedApplication] delegate]; //use the variable. } 
+1
source

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


All Articles