0

I'm trying to write a custom macro that performs some action when called it.

Here is my constants file:

#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]

and I created a pch file:

#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 */

and in my view controller, I'm trying to call the macro:

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    SHOW_VIEW;
}

And I see errors on the line SHOW_VIEW :

enter image description here

Teja Nandamuri
  • 11,045
  • 6
  • 57
  • 109
  • 3
    This is one of the reasons not to use macros for this, debugging them is nightmare. Use functions instead! – Hamish Jun 06 '16 at 18:33
  • do you mean calling them directly rather than using macros @originaluser2 – Teja Nandamuri Jun 06 '16 at 18:36
  • I mean using a C function to define these i.e `static inline void showView {...}`. You can then call it by doing `showView()` – Hamish Jun 06 '16 at 18:44
  • where do I declare these functions ? In AppDelegate ?@originaluser2 – Teja Nandamuri Jun 06 '16 at 18:45
  • 1
    You can define them in the same place as your macros (in place of them, in fact!) The advantage of using functions for this is that the compiler will actually treat it as compilable code, rather than just some gunk to copy and paste everywhere you refer to it. – Hamish Jun 06 '16 at 18:47

2 Answers2

2

Include your app delegate in the constants header MYGlobals.h

#import "AppDelegate.h"

I would urge to reconsider this method to get more readable and consistent code. Instead, what you can do is define a macro for your app delegate instance.

#define MY_APP_DELEGATE ((AppDelegate *)[UIApplication sharedApplication].delegate)

Then call its methods directly:

[MY_APP_DELEGATE showView];
[MY_APP_DELEGATE hideView];
Sonny Saluja
  • 7,193
  • 2
  • 25
  • 39
1

You are having syntax errors in your macro.

#define SHOW_VIEW [(AppDelegate*)[UIApplication sharedApplication].delegate showView];

#define HIDE_VIEW [(AppDelegate*)[UIApplication sharedApplication].delegate hideView];

Also you need to include the #import "AppDelegate.h" in the prefix.pch file.

Midhun MP
  • 103,496
  • 31
  • 153
  • 200
  • do we need ; at the endof line, for statements that start with #define ? – Teja Nandamuri Jun 06 '16 at 18:32
  • @TejaNandamuri: Actually `;` not for the #define it is for the code snippet you've written after that. When you use #define, the code you've written after the defined constant will be placed wherever you use it. That means the SHOW_VIEW will be replaced by `[(AppDelegate*)[UIApplication sharedApplication].delegate showView]`. In your case That lacks a `;` and compiler will throw error. You can write `SHOW_VIEW;` or define it the way mentioned in the answer – Midhun MP Jun 06 '16 at 18:35
  • ahh, I see. Good to know that. – Teja Nandamuri Jun 06 '16 at 18:38