Sim*_*nRH 4 import cocoa-touch objective-c header-files
我一直在观看一个视频,声明UIAlertView只有在导入UIKit.h时才有效.但是,如果我在头文件中注释掉import语句:
//#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
Run Code Online (Sandbox Code Playgroud)
当我将其添加到实现文件时,警报仍然有效:
- (void)viewDidLoad
{
[super viewDidLoad];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"Message" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
}
Run Code Online (Sandbox Code Playgroud)
请解释为什么这有效?UIKit的真正作用是什么?
这是因为它可能已在您的Prefix.pch文件中声明,默认情况下看起来像这样:
#ifdef __OBJC__
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#endif
Run Code Online (Sandbox Code Playgroud)