Why can I refer to UIKit classes after removing the UIKit.h import?

I watched a video saying that UIAlertView only works when importing UIKit.h. However, if I comment on the import statement in the header file:

 //#import <UIKit/UIKit.h> @interface ViewController : UIViewController @end 

the warning still works when I add it to the implementation file:

 - (void)viewDidLoad { [super viewDidLoad]; UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"Message" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; [alert show]; } 

Please explain why this works? What is the true role of UIKit?

+4
source share
2 answers

UIKit.h is just a file in which classes are defined, etc.

Without this, the compiler does not know what UIAlertView .

However, in your case, this probably works anyway, since #import <UIKit/UIKit.h> usually included in the project prefix file, which is included by default in your sources.

+3
source

This is because it is probably already declared in your Prefix.pch file, which by default looks something like this:

 #ifdef __OBJC__ #import <Foundation/Foundation.h> #import <UIKit/UIKit.h> #endif 
+8
source

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


All Articles