使用#define来替换代码
Use #define to replace code
我的iOS应用程序中有一段代码,我必须在每个视图中使用-不能在函数/方法中使用它-所以我想知道是否有任何方法可以使用#define并在需要的地方使用它的标识符。下面是示例代码:
我想用# define identifier
替换的代码[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(_gotECSlidingViewAnchorRightOrRightrNotification)
name:ECSlidingViewTopDidAnchorLeft
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(_gotECSlidingViewAnchorRightOrRightrNotification)
name:ECSlidingViewTopDidAnchorRight
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(_gotECSlidingViewTopDidResetNotification)
name:ECSlidingViewTopDidReset
object:nil];
所以我想知道我如何可以#定义它并在ViewDidLoad方法中使用它?
这并不能直接回答你的问题,但是作为一个处理过许多令人头疼的预处理器的老c++程序员,我建议不要使用#define来解决这个问题。
A couple options…
-
用你的两个选择器定义一个基类(来自UIViewController)。选择器可以在派生类中被重写。
@interface YourBaseCass: UIViewController
- (空白)viewDidLoad;//把你的add observer逻辑放在这里
- (空白)_gotECSlidingViewAnchorRightOrRightrNotification;
- (空白)_gotECSlidingViewTopDidResetNotification;
@end
@ implementation YourBaseCass
- (void)viewDidLoad //make sure you call me from the derived class { [super viewDidLoad] [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_gotECSlidingViewAnchorRightOrRightrNotification) name:ECSlidingViewTopDidAnchorLeft object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_gotECSlidingViewAnchorRightOrRightrNotification) name:ECSlidingViewTopDidAnchorRight object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_gotECSlidingViewTopDidResetNotification) name:ECSlidingViewTopDidReset object:nil]; }
@end
-
把你的功能放在一个全局静态方法(如果子类不是你的东西)。这将更容易调试。
-
(void) addObserversForObject:对象(id) {[[NSNotificationCenter defaultCenter] addObserver:object选择器:@ selector (_gotECSlidingViewAnchorRightOrRightrNotification)名称:ECSlidingViewTopDidAnchorLeft对象:零];
[[NSNotificationCenter defaultCenter] addObserver:object selector:@selector(_gotECSlidingViewAnchorRightOrRightrNotification) name:ECSlidingViewTopDidAnchorRight object:nil]; [[NSNotificationCenter defaultCenter] addObserver:object selector:@selector(_gotECSlidingViewTopDidResetNotification) name:ECSlidingViewTopDidReset object:nil];
}
-
你可以这样做:
在每行末尾的空格后面加上,这样可以防止编译器检查是否有新行或按下回车键。这使得代码可读。然后你可以在你的方法的任何地方使用MY_NOTIFICATIONS。
#define MY_NOTIFICATIONS [[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(_gotECSlidingViewAnchorRightOrRightrNotification)
name:ECSlidingViewTopDidAnchorLeft
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector (_gotECSlidingViewAnchorRightOrRightrNotification)
name:ECSlidingViewTopDidAnchorRight
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(_gotECSlidingViewTopDidResetNotification)
name:ECSlidingViewTopDidReset
object:nil];
这应该可以达到目的,前提是代码应该与此完全相同。否则你可以给#define
添加参数来改变它的一些东西
#define identifier do{ [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_gotECSlidingViewAnchorRightOrRightrNotification) name:ECSlidingViewTopDidAnchorLeft object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_gotECSlidingViewAnchorRightOrRightrNotification) name:ECSlidingViewTopDidAnchorRight object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_gotECSlidingViewTopDidResetNotification) name:ECSlidingViewTopDidReset object:nil];
} while (0);
不要依赖预处理器。这是一种糟糕的风格,经常会导致微妙的混淆。而且你仍然需要在所有这些文件中#include宏。
相反,定义一个类来处理通知@interface ECSReceptionist
- (id) initFor: (id) observer;
@end
对于一个类来说,这可能看起来有点轻量,但是您可以稍后给它赋予职责。例如,接待员也可以自己注册通知,并自主处理一些日常琐事。
避免预处理器:
- 让你的意图更清晰
- 避免了微妙的语法错误,节省了完整性
- 使调试更容易
- 为进一步重构 提供了机会
- 为单元测试提供了一个测试钩子
- 如何通过替换顺序代码的while循环来添加OpenMP for循环
- 如何替换此示例代码片段中已弃用的handler_type_t或 boost::asio::handler_type?
- 使用基于数组和范围的 For 循环替换一些基本代码行
- 什么可以用来替换代码中的循环和 if 语句?
- 用最少的代码行数替换c++中的一组字符
- 如何用符合C++核心准则的代码替换C风格的字符串解析
- C++代码在 for 循环的条件下给出运行时错误,而如果它被具有相同意义的代码替换,则编译正确
- 如何在C++代码中灵活使用和替换标准::shared_ptr或标准::unique_ptr或原始指针?
- TicTacToe代码不会用O和X替换数字
- 基于中间字母替换(反转)代码
- 有什么方法可以用本机C 代码替换Windows API计时器(settimer)
- 如何对 SFINAE 进行"deep",即当替换导致代码中进一步出现一些编译错误时?
- 这个给定的代码应该将给定的数字转换为尽可能滞后的数字,no.by 用 9.It 替换合适的数字是行不通的
- 将方法传递给方法并替换(几乎完全相同)重复代码
- 为什么代码中的模板参数推导/替换失败?-.
- 替换 C 代码宏并在C++代码中定义常量
- 试图用Boost::Beast替换我的libwebsocket代码
- 使用宏替换略有差异的重复代码块
- 用无分支代码替换if-else语句
- 我可以将main()中的代码替换为全局对象的构造函数吗?