Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I've been watching a video that stated that UIAlertView works only if UIKit.h has been imported. However, if I comment out the import statement in the header file:

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

the alert 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?

like image 504
SimonRH Avatar asked Dec 20 '22 22:12

SimonRH


2 Answers

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

#ifdef __OBJC__
    #import <Foundation/Foundation.h>
    #import <UIKit/UIKit.h>
#endif
like image 79
esqew Avatar answered Dec 28 '22 07:12

esqew


UIKit.h is just the file where the classes etc. are defined.

Without it, the compiler wouldn't know what UIAlertView is.

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

like image 30
houbysoft Avatar answered Dec 28 '22 07:12

houbysoft