使用#define来替换代码

Use #define to replace code

本文关键字:代码 替换 #define 使用      更新时间:2023-10-16

我的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…

  1. 用你的两个选择器定义一个基类(来自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

  2. 把你的功能放在一个全局静态方法(如果子类不是你的东西)。这将更容易调试。

    • (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

对于一个类来说,这可能看起来有点轻量,但是您可以稍后给它赋予职责。例如,接待员也可以自己注册通知,并自主处理一些日常琐事。

避免预处理器:

  • 让你的意图更清晰
  • 避免了微妙的语法错误,节省了完整性
  • 使调试更容易
  • 为进一步重构
  • 提供了机会
  • 为单元测试提供了一个测试钩子