使用#define替换代码

时间:2013-03-12 07:16:48

标签: c++ ios objective-c c

我的iOS应用程序中有一些代码,我必须在每个视图中使用它 - 不能在函数/方法中使用它 - 所以我想知道是否有任何方法我可以使用#define并使用它标识符所在的位置。以下是示例代码。

我想用#deinfe identifer取代的代码

[[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它并在ViewDidLoad方法中使用它?

4 个答案:

答案 0 :(得分:2)

这并没有直接回答你的问题,但作为一个处理过预处理器的许多令人头疼的老C ++程序员,我建议不要使用#define来实现这个目标。

几个选项......

  1. 使用两个选择器定义基类(从UIViewController派生)。可以在派生类中重写选择器。

    @interface YourBaseCass:UIViewController

    • (无效)viewDidLoad中; //把你的添加观察者逻辑放在这里
    • (无效)_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. 将您的功能放在全局静态方法中(如果子类化不是您的事情)。这将更容易调试。

    • (无效)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];
      

      }

答案 1 :(得分:1)

这应该可以解决问题,前提是代码应该完全相同。否则你可以把参数放到#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);

答案 2 :(得分:1)

您可以使用:

在每行末尾的空格后放置\,这可以防止编译器检查新行或输入按下的键。 这使代码可读。然后您可以在方法中的任何位置使用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]; \

答案 3 :(得分:0)

不要依赖预处理器。这是糟糕的风格,往往导致微妙的混乱。而且你仍然需要#include所有这些文件中的宏。

相反,定义一个类来处理通知

@interface ECSReceptionist
      - (id) initFor: (id) observer;
@end

对于一个类来说,这似乎有点轻量级,但您可以稍后赋予它职责。例如,接待员也可以自己注册通知并自动处理一些例行事务。

避免预处理器:

  • 让您的意图更清晰
  • 避免微妙的句法tsorus,拯救理智
  • 使调试更容易
  • 提供进一步重构的机会
  • 为您提供单元测试的测试钩子
相关问题