我正在尝试编写一个在调用时执行某些操作的自定义宏。
这是我的常量文件:
#ifdef __OBJC__
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#endif
#define SHOW_VIEW [(AppDelegate*)[UIApplication sharedApplication].delegate showView]
#define HIDE_VIEW [(AppDelegate*)[UIApplication sharedApplication].delegate hideView]
然后我创建了一个 pch 文件:
#ifndef MYPrefixHeader_pch
#define MYPrefixHeader_pch
// Include any system framework and library headers here that should be included in all compilation units.
// You will also need to set the Prefix Header build setting of one or more of your targets to reference this file.
#ifdef __OBJC__
#import "MYGlobals.h"
#endif
#endif /* MYPrefixHeader_pch */
在我的 View Controller 中,我尝试调用宏:
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
SHOW_VIEW;
}
我在 SHOW_VIEW 行看到错误:
最佳答案
在常量 header MYGlobals.h
中包含您的应用程序委托(delegate)
#import "AppDelegate.h"
我强烈建议重新考虑此方法以获得更具可读性和一致性的代码。相反,您可以为您的应用委托(delegate)实例定义一个宏。
#define MY_APP_DELEGATE ((AppDelegate *)[UIApplication sharedApplication].delegate)
然后直接调用它的方法:
[MY_APP_DELEGATE showView];
[MY_APP_DELEGATE hideView];
关于ios - 如何定义宏以在 ios 中执行操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37664397/